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 thing,
docs.Sort((x, y) => x.StoredDate.CompareTo(y.StoredDate)); docs.Reverse();
but is there a lambda expression to do it one step?
In the above example, StoredDate is a property typed as a DateTime.
You only need to call a. sort() without assigning it to a again. There is a built in function sorted() , which returns a sorted version of the list - a = sorted(a) will do what you want as well.
Here's how to sort unsorted dates: Drag down the column to select the dates you want to sort. Click Home tab > arrow under Sort & Filter, and then click Sort Oldest to Newest, or Sort Newest to Oldest.
To sort a Python date string list using the sort function, you'll have to convert the dates in objects and apply the sort on them. For this you can use the key named attribute of the sort function and provide it a lambda that creates a datetime object for each date and compares them based on this date object.
Though it's untested...
docs.Sort((x, y) => y.StoredDate.CompareTo(x.StoredDate));
should be the opposite of what you originally had.
What's wrong with:
docs.OrderByDescending(d => d.StoredDate);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With