Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>OP Edit: as many have correctly pointed out the original answer does not return the same collection, (originally focused more on sorting the dictionary part of the Q). Please see edit at bottom where I address the sorting of an observable collection. Original left here as still receiving up votes</strong></p> <p>You can use linq as the doSort method below illustrates. A quick code snippet: produces</p> <p>3:xey 6:fty 7:aaa</p> <p>Alternatively you could use an extension method on the collection itself</p> <pre><code>var sortedOC = _collection.OrderBy(i =&gt; i.Key); private void doSort() { ObservableCollection&lt;Pair&lt;ushort, string&gt;&gt; _collection = new ObservableCollection&lt;Pair&lt;ushort, string&gt;&gt;(); _collection.Add(new Pair&lt;ushort,string&gt;(7,"aaa")); _collection.Add(new Pair&lt;ushort, string&gt;(3, "xey")); _collection.Add(new Pair&lt;ushort, string&gt;(6, "fty")); var sortedOC = from item in _collection orderby item.Key select item; foreach (var i in sortedOC) { Debug.WriteLine(i); } } public class Pair&lt;TKey, TValue&gt; { 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; } } </code></pre> <p><strong>EDIT</strong></p> <p>To return an ObservableCollection, call .ToObservableCollection on <em>sortedOC</em> using e.g. <a href="http://www.extensionmethod.net/Details.aspx?ID=140" rel="noreferrer">this implementation</a>.</p> <p><strong>OP EDIT</strong> Sorting an observable and returning the same object sorted can be done using an extension method. For larger collections watch out for the number of collection changed notifications eg</p> <pre><code>public static void Sort&lt;T&gt;(this ObservableCollection&lt;T&gt; observable) where T : IComparable&lt;T&gt;, IEquatable&lt;T&gt; { List&lt;T&gt; sorted = observable.OrderBy(x =&gt; x).ToList(); int ptr = 0; while (ptr &lt; sorted.Count) { if (!observable[ptr].Equals(sorted[ptr])) { T t = observable[ptr]; observable.RemoveAt(ptr); observable.Insert(sorted.IndexOf(t), t); } else { ptr++; } } } </code></pre> <p>usage: Sample with an observer (used a Person class to keep it simple)</p> <pre><code>public class Person:IComparable&lt;Person&gt;,IEquatable&lt;Person&gt; { 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) &amp;&amp; this.Age.Equals(other.Age)) return true; return false; } } static void Main(string[] args) { Console.WriteLine("adding items..."); var observable = new ObservableCollection&lt;Person&gt;() { 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) =&gt; { 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(); }; </code></pre> <p>Output from above:<br /> removed Katy aged 51 at index 0<br /> added Katy aged 51 at index 8<br /> removed Mary aged 41 at index 3<br /> added Mary aged 41 at index 7<br /> removed Jane aged 20 at index 3<br /> added Jane aged 20 at index 5<br /> removed Jim aged 39 at index 3<br /> added Jim aged 39 at index 6<br /> removed Jane aged 20 at index 4<br /> added Jane aged 20 at index 5<br /></p> <p>The Person class implements both IComparable and IEquatable the latter is used to minimise the changes to the collection so as to reduce the number of change notifications raised </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload