Reputation: 81262
I have a collection of Task objects in a list.
List<Tasks> tasks = getTasks(); //This populates tasks
Each task item has a "dateCreated" field which is a fully fledged DateTime field.
Public class Task
{
DateTime dateCreated {get;set;}
//other props
}
My question is once I have a list of tasks, how can I resort the items in the collection based on this dateCreated field.
Thanks in advance.
Upvotes: 0
Views: 1319
Reputation: 235
best way to do that is
tasks = tasks.OrderBy(t => t.DateCreated.Ticks).ToList();
in this way list will be ordered by date and time
Upvotes: 0
Reputation: 20451
You dont need to implement IComparable<T>
to sort a list:
tasks.Sort((t1,t2) => t1.DateCreated.CompareTo(t2.DateCreated));
Upvotes: 1
Reputation: 190907
You can implement IComparable<T>
and use List<T>.Sort()
.
Or you could:
tasks.Sort(t => t.DateCreated);
http://msdn.microsoft.com/en-us/library/w56d4y5z.aspx
If you don't want to do that, just do:
tasks = tasks.OrderBy(t => t.DateCreated).ToList();
Upvotes: 3