Sort List Descending

后端 未结 4 1775
无人共我
无人共我 2020-12-13 17:19

In c# (3.0 or 3.5, so we can use lambdas), is there an elegant way of sorting a list of dates in descending order? I know I can do a straight sort and then reverse the whole

相关标签:
4条回答
  • 2020-12-13 17:43

    Though it's untested...

    docs.Sort((x, y) => y.StoredDate.CompareTo(x.StoredDate));
    

    should be the opposite of what you originally had.

    0 讨论(0)
  • 2020-12-13 17:47
    docs.Sort((x, y) => -x.StoredDate.CompareTo(y.StoredDate));
    

    Note the minus sign.

    0 讨论(0)
  • 2020-12-13 17:59
    docs.Sort((x, y) => y.StoredDate.CompareTo(x.StoredDate));
    

    Should do what you're looking for.

    0 讨论(0)
  • 2020-12-13 18:04

    What's wrong with:

    docs.OrderByDescending(d => d.StoredDate);
    
    0 讨论(0)
提交回复
热议问题