Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Off hand, I don't really see much you could do to make it faster, aside from avoiding calling this method as much as possible, as sorting and rebuilding lists is inherently slow :-). One possible option would be to use a SortedSet, which would keep the collection in sorted order and thereby help you avoid the costs associated with resorting every time you change the collection. Doesn't help much though I suppose when you're changing the sort strategy (although I'd bet the SortedSet implementers gave that some thought when they wrote the code).</p> <p>This doesn't really answer your question I suppose, but instead of passing an IComparer, could you instead pass a Func&lt;...> to your Order() method (and ditch the IndexOrdering enum), and then pass that Func straight into the OrderBy() method? This would allow you to express all sorts of crazy sort methods without having to touch this class, and you could remove any unnecessary state baggage (like _rnd, if it's not used anywhere else) from this class.</p> <p>Something like:</p> <pre><code>public class RatioBag : IList&lt;RatioAssociation&gt; { private readonly IList&lt;RatioAssociation&gt; _items = new List&lt;RatioAssociation&gt;(); public void Order(Func&lt;RatioAssociation, int&gt; func) { var ordered = _items.OrderBy(func); _items.Clear(); ((List&lt;RatioAssociation&gt;)_items).AddRange(ordered); } } </code></pre> <p>then, to call it:</p> <pre><code>var myCollection = new RatioBag(); myCollection.Order(item =&gt; item.PropertyX); myCollection.Order(item =&gt; item.PropertyY); var r = new Random(); myCollection.Order(item =&gt; r.Next()); </code></pre> <p>The IComparer solution would also achieve a similar solution.</p> <p>Hope this helps.</p> <p>Nate</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. This table or related slice is empty.
    1. 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