我有以下课程:
[DataContract] public class Pair<TKey, TValue> : INotifyPropertyChanged, IDisposable { public Pair(TKey key, TValue value) { Key = key; Value = value; } #region Properties [DataMember] public TKey Key { get { return m_key; } set { m_key = value; OnPropertyChanged("Key"); } } [DataMember] public TValue Value { get { return m_value; } set { m_value = value; OnPropertyChanged("Value"); } } #endregion #region Fields private TKey m_key; private TValue m_value; #endregion #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string name) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(name)); } } #endregion #region IDisposable Members public void Dispose() { } #endregion }
我把它放在一个ObservableCollection中:
ObservableCollection<Pair<ushort, string>> my_collection = new ObservableCollection<Pair<ushort, string>>(); my_collection.Add(new Pair(7, "aaa")); my_collection.Add(new Pair(3, "xey")); my_collection.Add(new Pair(6, "fty"));
问:如何按键排序?
OP EDIT 随后进行了修改,以显示对原始集合进行排序而不创建新集合。(原始答案可以在末尾找到)
可以使用可扩展方法对可观察对象进行排序并返回已排序的相同对象。对于较大的收藏,请注意收藏更改通知的数量,例如
public static void Sort<T>(this ObservableCollection<T> observable) where T : IComparable<T>, IEquatable<T> { List<T> sorted = observable.OrderBy(x => x).ToList(); int ptr = 0; while (ptr < sorted.Count) { if (!observable[ptr].Equals(sorted[ptr])) { T t = observable[ptr]; observable.RemoveAt(ptr); observable.Insert(sorted.IndexOf(t), t); } else { ptr++; } } }
用法:与观察者一起采样(使用Person类使其保持简单)
public class Person:IComparable<Person>,IEquatable<Person> { public string Name { get; set; } public int Age { get; set; } public int CompareTo(Person other) { if (this.Age == other.Age) return 0; return this.Age.CompareTo(other.Age); } public override string ToString() { return Name + " aged " + Age; } public bool Equals(Person other) { if (this.Name.Equals(other.Name) && this.Age.Equals(other.Age)) return true; return false; } } static void Main(string[] args) { Console.WriteLine("adding items..."); var observable = new ObservableCollection<Person>() { new Person { Name = "Katy", Age = 51 }, new Person { Name = "Jack", Age = 12 }, new Person { Name = "Bob", Age = 13 }, new Person { Name = "John", Age = 14 }, new Person { Name = "Mary", Age = 41 }, new Person { Name = "Jane", Age = 20 }, new Person { Name = "Jim", Age = 39 }, new Person { Name = "Sue", Age = 15 }, new Person { Name = "Kim", Age = 19 } }; //what do observers see? observable.CollectionChanged += (o, e) => { if (e.OldItems != null) { foreach (var item in e.OldItems) { Console.WriteLine("removed {0} at index {1}", item, e.OldStartingIndex); } } if (e.NewItems != null) { foreach (var item in e.NewItems) { Console.WriteLine("added {0} at index {1}", item, e.NewStartingIndex); } }}; Console.WriteLine("\nsorting items..."); observable.Sort(); };
从上面的输出: 删除了索引0的51岁的凯蒂, 添加了索引8的51岁的凯蒂, 删除了索引3的41岁的玛丽, 添加了索引7的41岁的玛丽, 删除了索引3的20岁的简, 添加了索引5的20岁的简, 删除了39岁的吉姆在索引3中 添加了在索引6中39岁的吉姆,在索引4中 删除了20 岁的简,在索引5中添加了20岁的简
Person类同时实现IComparable和IEquatable,后者用于最小化对集合的更改,从而减少引发的更改通知的数量
要返回ObservableCollection,请使用例如[此实现] [1]在 sortedOC 上调用.ToObservableCollection。
* orig answer *您可以使用linq,如下面的doSort方法所示。快速代码段:产生
3:xey 6:fty 7:aaa
或者,您可以对集合本身使用扩展方法
var sortedOC = _collection.OrderBy(i => i.Key); private void doSort() { ObservableCollection<Pair<ushort, string>> _collection = new ObservableCollection<Pair<ushort, string>>(); _collection.Add(new Pair<ushort,string>(7,"aaa")); _collection.Add(new Pair<ushort, string>(3, "xey")); _collection.Add(new Pair<ushort, string>(6, "fty")); var sortedOC = from item in _collection orderby item.Key select item; foreach (var i in sortedOC) { Debug.WriteLine(i); } } public class Pair<TKey, TValue> { private TKey _key; public TKey Key { get { return _key; } set { _key = value; } } private TValue _value; public TValue Value { get { return _value; } set { _value = value; } } public Pair(TKey key, TValue value) { _key = key; _value = value; } public override string ToString() { return this.Key + ":" + this.Value; } }