C#涓璴ist闆嗗悎Sort鏂规硶鎬庝箞浣跨敤
鍦–#涓紝List闆嗗悎鐨凷ort鏂规硶鍙互鎺ュ彈涓€涓疄鐜颁簡IComparer鎺ュ彛鐨勫璞★紝鎴栬€呴€氳繃Lambda琛ㄨ揪寮忎紶閫掍竴涓瘮杈冨嚱鏁版潵杩涜鎺掑簭銆?/p>
浣跨敤IComparer鎺ュ彛鐨勬柟寮忥細
public class CustomComparer : IComparer<int>
{
public int Compare(int x, int y)
{
// 鑷畾涔夋帓搴忛€昏緫
return x.CompareTo(y);
}
}
List<int> numbers = new List<int> { 3, 1, 2 };
numbers.Sort(new CustomComparer());
浣跨敤Lambda琛ㄨ揪寮忕殑鏂瑰紡锛?/p>
List<int> numbers = new List<int> { 3, 1, 2 };
numbers.Sort((x, y) => x.CompareTo(y));
閫氳繃浠ヤ笂涓ょ鏂瑰紡锛岄兘鍙互瀹炵幇瀵筁ist闆嗗悎杩涜鎺掑簭銆?/p>