TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Sort DateTime List

This C# example program shows how to sort a List of DateTimes. The values are sorted chronologically.

Sort DateTime list. A List of DateTimes can be sorted.

We can base this on the year or the month. It is sometimes necessary to sort DateTimes from most recent to least recent, and also vice versa. Sorting based on the month is also useful.

Example. First, this program creates List of four DateTime values. Next, it calls the four Sort custom methods. The Sort custom methods internally call the List.Sort method. They provide a Comparison delegate as a lambda expression.

Tip: The Comparison implementation returns the result of the CompareTo method on the two arguments.

Comparison

C# program that sorts List of DateTimes

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
	var list = new List<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));

	Display(SortAscending(list), "SortAscending");
	Display(SortDescending(list), "SortDescending");
	Display(SortMonthAscending(list), "SortMonthAscending");
	Display(SortMonthDescending(list), "SortMonthDescendeing");

    }

    static List<DateTime> SortAscending(List<DateTime> list)
    {
	list.Sort((a, b) => a.CompareTo(b));
	return list;
    }

    static List<DateTime> SortDescending(List<DateTime> list)
    {
	list.Sort((a, b) => b.CompareTo(a));
	return list;
    }

    static List<DateTime> SortMonthAscending(List<DateTime> list)
    {
	list.Sort((a, b) => a.Month.CompareTo(b.Month));
	return list;
    }

    static List<DateTime> SortMonthDescending(List<DateTime> list)
    {
	list.Sort((a, b) => b.Month.CompareTo(a.Month));
	return list;
    }

    static void Display(List<DateTime> list, string message)
    {
	Console.WriteLine(message);
	foreach (var datetime in list)
	{
	    Console.WriteLine(datetime);
	}
	Console.WriteLine();
    }
}

Output

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

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

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

SortMonthDescendeing
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

The Ascending methods order the data from low to high, and the Descending methods do the opposite. Also, the SortMonth methods sort based on the month value of the DateTimes. More information about DateTime is available.

DateTimeDateTime.Month

Summary. We looked at a way you can sort a List of DateTime instances in your C# program. It is also possible to sort with a query expression. This syntax might be clearer for some programmers.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf