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 Module
SORT: 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