您有一個通用List
您的程式碼中存在兩個問題:
這是修正後的自訂比較器:
public class MyComparer : IComparer<Object> { public int Compare(Object stringA, Object stringB) { string[] valueA = stringA.ToString().Split('/'); string[] valueB = stringB.ToString().Split('/'); if (valueA.Length != 2 || valueB.Length != 2) { return String.Compare(stringA.ToString(), stringB.ToString()); } // Note: do error checking and consider i18n issues too :) if (valueA[0] == valueB[0]) { return int.Parse(valueA[1]).CompareTo(int.Parse(valueB[1])); } else { return int.Parse(valueA[0]).CompareTo(int.Parse(valueB[0])); } } }
這裡是更新 BindingList
case ListSortDirection.Ascending: MyComparer comparer = new MyComparer(); items = items.OrderByDescending(x => property.GetValue(x), comparer).ToList(); break; default: // Handle descending sort direction here break; } this.Items = items;
以上是如何使用自訂 IComparer 和 Linq OrderBy 對清單進行正確排序?的詳細內容。更多資訊請關注PHP中文網其他相關文章!