ObservableCollection排序

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);
    }
}
View Code

 

posted @ 2017-10-13 16:56  一!雨  阅读(2003)  评论(0编辑  收藏  举报