Note that there are some explanatory texts on larger screens.

plurals
  1. POSort a GenericList<T> by date property
    primarykey
    data
    text
    <p>I can't figure out how to do this, these two questions aren't working for me. One says I can't use the delegate and the comparer, and the other says that it can't convert the lambda to a delegate.</p> <p><a href="https://stackoverflow.com/questions/2116754/sort-generic-list">Sort Generic List</a></p> <p><a href="https://stackoverflow.com/questions/177373/how-do-i-sort-a-generic-list">How do I sort a generic list?</a></p> <p>I have a <code>GenericList&lt;T&gt;</code> of <code>ScheduleItem</code>. Each item has a <code>PaymentDate</code> property which is a <code>DateTime?</code>.</p> <p>I basically want to sort this list on that property (old->newest). What's the easiest way to do this?</p> <p>First I tried this:</p> <pre><code>transaction.ScheduleCollection.Sort(delegate(ScheduleItem p1, ScheduleItem p2) { return p1.PaymentDate.CompareTo(p2.PaymentDate); }); </code></pre> <p>And I get these two errors:</p> <blockquote> <p>Cannot convert anonymous method to type 'System.Collections.IComparer' because it is not a delegate type</p> <p>'System.Nullable' does not contain a definition for 'CompareTo' and no extension method 'CompareTo' accepting a first argument of type 'System.Nullable' could be found (are you missing a using directive or an assembly reference?)</p> </blockquote> <p>And the other one I tried was this:</p> <pre><code>transaction.ScheduleCollection.Sort((lhs, rhs) =&gt; (lhs.PaymentDate.CompareTo(rhs.PaymentDate))); </code></pre> <p>and get</p> <blockquote> <p>Cannot convert anonymous method to type 'System.Collections.IComparer' because it is not a delegate type</p> </blockquote> <pre><code>public void Sort(IComparer comparer); </code></pre> <p>That's the sort method I have to work with.</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.
 

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