배열을 선언하고 초기화 -
int[] arr = new int[] {
87,
23,
65,
29,
67
}; 정렬하려면 Sort() 메서드와 CompareTo()를 사용하여 내림차순으로 비교하고 표시합니다. -
Array.Sort < int > (arr, new Comparison < int > ((val1, val2) => val2.CompareTo(val1)));
전체 코드를 보자 -
예시
using System;
using System.Collections.Generic;
using System.Text;
public class Demo {
public static void Main(string[] args) {
int[] arr = new int[] {
87,
23,
65,
29,
67
};
// Initial Array
Console.WriteLine("Initial Array...");
foreach(int items in arr) {
Console.WriteLine(items);
}
Array.Sort < int > (arr, new Comparison < int > ((val1, val2) => val2.CompareTo(val1)));
// Sorted Array
Console.WriteLine("Sorted Array in decreasing order...");
foreach(int items in arr) {
Console.WriteLine(items);
}
}
} 출력
Initial Array... 87 23 65 29 67 Sorted Array in decreasing order... 87 67 65 29 23