title: Sorting a list wrapper class
body: <p>I have a class that wraps a List and adds functionality for dealing with ratios. It needs to be sorted two ways at different times; by ordinal position, and by the amount of the ratio.</p>
  
  <p>Below is the method I am currently using to do the sort by ordinal position which I want to use as the basis for two related questions:</p>
  
  <ol>
  <li>Is there a more perfomant way to do this?</li>
  <li>How would I do this using an IComparer?</li>
  </ol>
  
  <p>The primary motivation for passing in the IComparer would be to allow for different implementations to randomly shuffle the list. <a href="https://stackoverflow.com/questions/557911/shuffle-using-icomparer">This post seems to address the same issue</a> but without any resolution in code that I can see. I'm looking for some sample code and usage of it so I can wrap my head around it.</p>
  
  <p>Cheers,<br>
  Berryl</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Maintains a list of ratios suitable for use in an allocation when the
  /// sum of all items is exactly equal to one (100%). No individual it may
  /// be less than zero or greater than one.
  /// &lt;/summary&gt;
  public class RatioBag : IList&lt;RatioAssociation&gt;
  {
      private readonly IList&lt;RatioAssociation&gt; _items = new List&lt;RatioAssociation&gt;();
  
      private Random _rnd;
  
      public void Order(IndexOrdering ordering) {
          IList&lt;RatioAssociation&gt; ordered = null;
          switch (ordering)
          {
              case IndexOrdering.FirstToLast:
                  break;
              case IndexOrdering.LastToFirst:
                  ordered = this.Reverse().ToList();
                  break;
              case IndexOrdering.Random:
                  if (_rnd == null) _rnd = new Random();
                  ordered = Enumerable.Range(0, Count)
                      .OrderBy(x =&gt; _rnd.Next())
                      .Select(index =&gt; this[index])
                      .ToList();
                  break;
              default:
                  throw new ArgumentOutOfRangeException("ordering");
          }
          if (ordered == null) return;
  
          Clear();
          foreach (var ra in ordered) {
              Add(ra);
          }
      }
  </code></pre>