之前用到的一段代码,记录一下
1 public static class ObservableExtension 2 { 3 public static void Sort<TSource, TKey>(this Collection<TSource> source, Func<TSource, TKey> keySelector) 4 { 5 List<TSource> sortedList = source.OrderBy(keySelector).ToList(); 6 source.Clear(); 7 foreach (var sortedItem in sortedList) 8 source.Add(sortedItem); 9 } 10 11 public static void Sort<T>(this ObservableCollection<T> collection) where T : IComparable 12 { 13 List<T> sortedList = collection.OrderBy(x => x).ToList(); 14 for(int i = 0;i<sortedList.Count();i++) 15 { 16 collection.Move(collection.IndexOf(sortedList[i]), i); 17 } 18 } 19 }