开发者

Sorting lists by date and then order by latest

开发者 https://www.devze.com 2022-12-15 20:10 出处:网络
I have a list and would like to sort 开发者_如何学Goby date then order by latest date first. The code below sorts by date:

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));
0

精彩评论

暂无评论...
验证码 换一张
取 消