Lorenzo B
Lorenzo B

Reputation: 33428

How to order a list by date (and/or minute and seconds) in C#

I need to order a list of elements by date.

This lamba function works well, but if two elements have the same date it doesn't.

orderList.Sort((x, y) => y.Value.CreationDate.CompareTo(x.Value.CreationDate));

where CreationDate is a DateTime value.

Suppose, for example, that two elements have the same creation date (09/05/2011). I would be able to distinguish minute (or maybe seconds).

Thank you in advance.

Upvotes: 0

Views: 833

Answers (2)

Blau
Blau

Reputation: 5762

You can use DateTime Struct

As it implement IComparable you don't need that lambda, only call orederList.Sort()

Upvotes: 0

Maxim V. Pavlov
Maxim V. Pavlov

Reputation: 10509

DateTime already contains the hours, minutes and seconds. If they are not available in your CreationDate value, then there is nothing you can sort based on. If, in fact, the data is there, than you should have no problem sorting as it is.

Upvotes: 1

Related Questions