Sort ObservableCollection 通过C#

问题描述 投票:43回答:12

我低于ObservableCollection<string>。我需要按字母顺序[

private ObservableCollection<string> _animals = new ObservableCollection<string> { "Cat", "Dog", "Bear", "Lion", "Mouse", "Horse", "Rat", "Elephant", "Kangaroo", "Lizard", "Snake", "Frog", "Fish", "Butterfly", "Human", "Cow", "Bumble Bee" };
我尝试过_animals.OrderByDescending。但是我不知道如何正确使用它。

_animals.OrderByDescending(a => a.<what_is_here_?>);

我该怎么做?
c# sorting observablecollection
12个回答
102
投票
简介

1
投票
/// <summary> /// Inserts an item into a list in the correct place, based on the provided key and key comparer. Use like OrderBy(o => o.PropertyWithKey). /// </summary> public static void InsertInPlace<TItem, TKey>(this ObservableCollection<TItem> collection, TItem itemToAdd, Func<TItem, TKey> keyGetter) { int index = collection.ToList().BinarySearch(keyGetter(itemToAdd), Comparer<TKey>.Default, keyGetter); collection.Insert(index, itemToAdd); }

0
投票
我对某个类别的字段(距离)进行了排序。

0
投票
这里是Shimmy的稍有变化,用于收集已经实现了众所周知的Move的类。在这种情况下,“ order by”选择器是隐式的。

39
投票
我知道这是一个古老的问题,但是“ sort observablecollection”的第一个Google搜索结果是Google的结果,因此我认为值得离开我的两分钱。

9
投票
我创建了ObservableCollection的扩展方法

7
投票
我看着这些东西,得到了整理,然后像上面那样打破了装订。提出了这个解决方案,尽管它比大多数解决方案简单,但似乎可以满足我的要求,,,

7
投票
这是一个public static void MySort<TSource,TKey>(this ObservableCollection<TSource> observableCollection, Func<TSource, TKey> keySelector) { var a = observableCollection.OrderBy(keySelector).ToList(); observableCollection.Clear(); foreach(var b in a) { observableCollection.Add(b); } } ,它会根据更改自动对自身进行排序,仅在必要时才触发排序,并且仅触发单个move collection更改操作。

3
投票
using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; using System.Linq; namespace ConsoleApp4 { using static Console; public class SortableObservableCollection<T> : ObservableCollection<T> { public Func<T, object> SortingSelector { get; set; } public bool Descending { get; set; } protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) { base.OnCollectionChanged(e); if (SortingSelector == null || e.Action == NotifyCollectionChangedAction.Remove || e.Action == NotifyCollectionChangedAction.Reset) return; var query = this .Select((item, index) => (Item: item, Index: index)); query = Descending ? query.OrderBy(tuple => SortingSelector(tuple.Item)) : query.OrderByDescending(tuple => SortingSelector(tuple.Item)); var map = query.Select((tuple, index) => (OldIndex:tuple.Index, NewIndex:index)) .Where(o => o.OldIndex != o.NewIndex); using (var enumerator = map.GetEnumerator()) if (enumerator.MoveNext()) Move(enumerator.Current.OldIndex, enumerator.Current.NewIndex); } } //USAGE class Program { static void Main(string[] args) { var xx = new SortableObservableCollection<int>() { SortingSelector = i => i }; xx.CollectionChanged += (sender, e) => WriteLine($"action: {e.Action}, oldIndex:{e.OldStartingIndex}," + " newIndex:{e.NewStartingIndex}, newValue: {xx[e.NewStartingIndex]}"); xx.Add(10); xx.Add(8); xx.Add(45); xx.Add(0); xx.Add(100); xx.Add(-800); xx.Add(4857); xx.Add(-1); foreach (var item in xx) Write($"{item}, "); } } } 的参数是一个返回要排序的键的函数。在您的情况下,关键是字符串本身:

3
投票
var result = _animals.OrderByDescending(a => a);

3
投票
_animals.OrderByDescending(a => a.<what_is_here_?>);

2
投票
此扩展方法无需对整个列表进行排序。
© www.soinside.com 2019 - 2024. All rights reserved.