Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, since you are using <code>List&lt;T&gt;</code> it would be a lot simpler to just use a <code>Comparison&lt;T&gt;</code>, for example:</p> <pre><code>List&lt;Foo&gt; data = ... // sort by name descending data.Sort((x,y) =&gt; -x.Name.CompareTo(y.Name)); </code></pre> <p>Of course, with LINQ you could just use:</p> <pre><code>var ordered = data.OrderByDescending(x=&gt;x.Name); </code></pre> <p>But you can re-introduce this in <code>List&lt;T&gt;</code> (for in-place re-ordering) quite easily; Here's an example that allows <code>Sort</code> on <code>List&lt;T&gt;</code> with lambda syntax:</p> <pre><code>using System; using System.Collections.Generic; class Foo { // formatted for vertical space public string Bar{get;set;} } static class Program { static void Main() { List&lt;Foo&gt; data = new List&lt;Foo&gt; { new Foo {Bar = "abc"}, new Foo {Bar = "jkl"}, new Foo {Bar = "def"}, new Foo {Bar = "ghi"} }; data.SortDescending(x =&gt; x.Bar); foreach (var row in data) { Console.WriteLine(row.Bar); } } static void Sort&lt;TSource, TValue&gt;(this List&lt;TSource&gt; source, Func&lt;TSource, TValue&gt; selector) { var comparer = Comparer&lt;TValue&gt;.Default; source.Sort((x,y)=&gt;comparer.Compare(selector(x),selector(y))); } static void SortDescending&lt;TSource, TValue&gt;(this List&lt;TSource&gt; source, Func&lt;TSource, TValue&gt; selector) { var comparer = Comparer&lt;TValue&gt;.Default; source.Sort((x,y)=&gt;comparer.Compare(selector(y),selector(x))); } } </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. 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.
    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