Home
VB.NET
Sort DateTime List
This page was last reviewed on Dec 2, 2023.
Dot Net Perls
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.
List
DateTime
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.
Example. 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.
Lambda
Step 1 We create a List of DateTimes by calling List Add 4 times. Each DateTime has a different year, and a different month number.
List Add
Step 2 Here we call the 4 different sorting methods. We pass the resulting 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 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
Ascending, descending. 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.
Also For months, we access the Month property on each DateTime being compared, and use that with CompareTo.
CompareTo
Summary. DateTimes can be sorted—the default comparison orders them from earliest to latest (chronologically). But with lambda expressions, we can modify this ordering.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 2, 2023 (new).
Home
Changes
© 2007-2024 Sam Allen.