ObservableCollection没有自带的sort排序功能,那么可以写一个扩展方法:
public static void Sort<T>(this ObservableCollection<T> collection) { collection.Sort(Comparer<T>.Default); } public static void Sort<T>(this ObservableCollection<T> collection, IComparer<T> comparer) { if (collection == null || collection.Count <= 1) return; var lst = collection.ToList(); lst.Sort(comparer); var count = collection.Count; for (int m = 0; m < count; m++) { var dex = collection.IndexOf(lst[m]); if (dex == m) continue; collection.Move(dex, m); } }