Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem with sorting an ObservableCollection is that every time you change the collection, an event will get fired off. So for a sort that is removing items from one position and adding them to another, you will end up having a ton of events firing.</p> <p>I think you're best bet is to just insert the stuff into the ObservableCollection in the proper order to begin with. Removing items from the collection won't effect ordering. I whipped up a quick extension method to illustrate</p> <pre><code> public static void InsertSorted&lt;T&gt;(this ObservableCollection&lt;T&gt; collection, T item, Comparison&lt;T&gt; comparison) { if (collection.Count == 0) collection.Add(item); else { bool last = true; for (int i = 0; i &lt; collection.Count; i++) { int result = comparison.Invoke(collection[i], item); if (result &gt;= 1) { collection.Insert(i, item); last = false; break; } } if (last) collection.Add(item); } } </code></pre> <p>So if you were to use strings (for instance), the code would look like this</p> <pre><code> ObservableCollection&lt;string&gt; strs = new ObservableCollection&lt;string&gt;(); Comparison&lt;string&gt; comparison = new Comparison&lt;string&gt;((s1, s2) =&gt; { return String.Compare(s1, s2); }); strs.InsertSorted("Mark", comparison); strs.InsertSorted("Tim", comparison); strs.InsertSorted("Joe", comparison); strs.InsertSorted("Al", comparison); </code></pre> <p><strong>Edit</strong></p> <p>You can keep the calls identical if you extend the ObservableCollection and supply your own insert/add methods. Something like this:</p> <pre><code>public class BarDataCollection : ObservableCollection&lt;BarData&gt; { private Comparison&lt;BarData&gt; _comparison = new Comparison&lt;BarData&gt;((bd1, bd2) =&gt; { return DateTime.Compare(bd1.StartDate, bd2.StartDate); }); public new void Insert(int index, BarData item) { InternalInsert(item); } protected override void InsertItem(int index, BarData item) { InternalInsert(item); } public new void Add(BarData item) { InternalInsert(item); } private void InternalInsert(BarData item) { if (Items.Count == 0) Items.Add(item); else { bool last = true; for (int i = 0; i &lt; Items.Count; i++) { int result = _comparison.Invoke(Items[i], item); if (result &gt;= 1) { Items.Insert(i, item); last = false; break; } } if (last) Items.Add(item); } } } </code></pre> <p>The insert index is ignored.</p> <pre><code> BarData db1 = new BarData(DateTime.Now.AddDays(-1)); BarData db2 = new BarData(DateTime.Now.AddDays(-2)); BarData db3 = new BarData(DateTime.Now.AddDays(1)); BarData db4 = new BarData(DateTime.Now); BarDataCollection bdc = new BarDataCollection(); bdc.Add(db1); bdc.Insert(100, db2); bdc.Insert(1, db3); bdc.Add(db4); </code></pre>
    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. 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