c#中ObservableCollection排序方法


之前用到的一段代码,记录一下

 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     }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM