Sort
List
, DateTimes
How can you sort DateTimes
? Suppose we want to order a List
of DateTimes
in VB.NET from earliest to latest, or even by their month.
With the List
Sort
subroutine, we can specify a lambda expression that compares each element. Then the List
is sorted in-place. For DateTimes
, we can use List
Sort
.
We have 4 Sort
Functions in this Module
—each one sorts a List
of DateTimes
in a specific way. The Sort()
List
function is used with a different lambda each time.
List
of DateTimes
by calling List
Add 4 times. Each DateTime
has a different year, and a different month number.List
to Display()
, which writes it to the console.Module Module1 Sub Main() ' Step 1: create a List of DateTimes. Dim list = New List(Of DateTime)() list.Add(New DateTime(1980, 5, 5)) list.Add(New DateTime(1982, 10, 20)) list.Add(New DateTime(1984, 1, 4)) list.Add(New DateTime(1979, 6, 19)) ' Step 2: call sorting methods, and display the different kinds of sorts. Display(SortAscending(list), "SortAscending") Display(SortDescending(list), "SortDescending") Display(SortMonthAscending(list), "SortMonthAscending") Display(SortMonthDescending(list), "SortMonthDescending") End Sub Function SortAscending(list as List(Of DateTime)) As List(Of DateTime) ' Call lambda function to the sort the list. list.Sort(Function(a, b) Return a.CompareTo(b) End Function) Return list End Function Function SortDescending(list as List(Of DateTime)) As List(Of DateTime) ' Reverses the order of comparisons for reverse sort. list.Sort(Function(a, b) Return b.CompareTo(a) End Function) Return list End Function Function SortMonthAscending(list as List(Of DateTime)) As List(Of DateTime) ' Specify Month property for sort. list.Sort(Function(a, b) Return a.Month.CompareTo(b.Month) End Function) Return list End Function Function SortMonthDescending(list as List(Of DateTime)) As List(Of DateTime) ' Reverse sort on Month property. list.Sort(Function(a, b) Return b.Month.CompareTo(a.Month) End Function) Return list End Function Sub Display(list as List(Of DateTime), message as String) ' Display the sorting results. Console.WriteLine("SORT: {0}", message) For Each datetime in list Console.WriteLine(datetime) Next End Sub End ModuleSORT: SortAscending 6/19/1979 12:00:00 AM 5/5/1980 12:00:00 AM 10/20/1982 12:00:00 AM 1/4/1984 12:00:00 AM SORT: SortDescending 1/4/1984 12:00:00 AM 10/20/1982 12:00:00 AM 5/5/1980 12:00:00 AM 6/19/1979 12:00:00 AM SORT: SortMonthAscending 1/4/1984 12:00:00 AM 5/5/1980 12:00:00 AM 6/19/1979 12:00:00 AM 10/20/1982 12:00:00 AM SORT: SortMonthDescending 10/20/1982 12:00:00 AM 6/19/1979 12:00:00 AM 5/5/1980 12:00:00 AM 1/4/1984 12:00:00 AM
For an ascending sort, we compare the first argument to the second argument in the lambda passed to List
Sort
. And for descending, we reverse the order of arguments.
DateTime
being compared, and use that with CompareTo
.DateTimes
can be sorted—the default comparison orders them from earliest to latest (chronologically). But with lambda expressions, we can modify this ordering.