views:

55

answers:

1

Hi,

I have a list and would like to sort by 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

+2  A: 

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));
Jason
im loving it. Thanks!
test
You're welcome.
Jason