You have a generic List
There are two issues in your code:
Here is the corrected custom comparer:
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])); } } }
And here is the corrected code to update the 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;
The above is the detailed content of How to Correctly Sort a List Using a Custom IComparer with Linq OrderBy?. For more information, please follow other related articles on the PHP Chinese website!