title: How to return a sorted collection to ListView?
body: <p>I have data (a 'DogWalk') that I'm accessing with EF. At runtime, I'm determining the distance of the DogWalk relative to an inputted postcode. This works fine, however, I'm having trouble returning an IEnumerable <strong>sorted</strong> collection to the <code>ListView</code>.</p>
  
  <p>The code is part of the method <code>public IEnumerable&lt;DogWalk&gt; ListView1_GetData()</code>. I have a list which contains all the walks</p>
  
  <pre><code>List&lt;DogWalk&gt; grabAllWalks = (from w in db.DogWalks.Include("Pictures")
                                        select w).ToList();
  </code></pre>
  
  <p>I loop through the walks and add them to a struct, which stores <code>{double distance, DogWalk theWalk}</code>. I also create a List to store the anonymous struct objects:</p>
  
  <pre><code>var inRangeWalks = new List&lt;InRangeWalks&gt;();
  foreach (var walk in grabAllWalks)
    {
     double dis = ....;
  
     if (dis &lt;= radius) //if within range, add to list
     {
       inRangeWalks.Add(new InRangeWalks(dis, walk));
     }
  }
  </code></pre>
  
  <p>I now sort the List based on postcodes distances:</p>
  
  <pre><code>inRangeWalks.Sort((x, y) =&gt; x.DistanceFromPostcode.CompareTo(y.DistanceFromPostcode));
  </code></pre>
  
  <p>The problem is that I need to return an <code>IEnumerable</code> collection. I could do something like: </p>
  
  <pre><code>IEnumerable&lt;DogWalk&gt; sortedWalks = inRangeWalks.Select(x =&gt; x.Walk); 
  </code></pre>
  
  <p>But with IEnumerable <a href="https://stackoverflow.com/questions/10409893/ienumerable-and-order">not guaranteeing</a> insertion order, this could be problematic. </p>
  
  <p>What are my options? </p>