I have a list and would like to sort 开发者_如何学Goby date then order by latest date first.
The code below sorts by date:
allFeeds.Sort(delegate(SimplifiedFeedItem p1, SimplifiedFeedItem p2)
{ return p1.PublishDate.CompareTo(p2.PublishDate); });
allFeeds.ForEach(delegate(SimplifiedFeedItem p)
{ // display list});
How do I then order it by the latest date first (its ordering by the oldest date first currently)? Thanks
Just change the sign that is returned from DateTime.CompareTo
:
allFeeds.Sort(delegate(SimplifiedFeedItem p1, SimplifiedFeedItem p2)
{ return -p1.PublishDate.CompareTo(p2.PublishDate); });
You can make this cleaner using a lambda expression:
allFeeds.Sort((p1, p2) => -p1.PublishDate.CompareTo(p2.PublishDate));
精彩评论