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