C# – Using a list of tuples

Here’s an example of how to initialize a list of named tuples:

var movies = new List<(string title, int year)>()
{
    ("Jurassic Park", 1993),
    ("Starship Troopers", 1997),
    ("The Matrix", 1999)
};

foreach(var movie in movies)
{
    Console.WriteLine($"{movie.title} was released in year {movie.year}");
}
Code language: C# (cs)

This creates a list of named value tuples (ValueTuple<string, int>) using list initializer syntax and then loops through the tuples. I suggest always using named value tuples (not System.Tuple).

This outputs the following:

Jurassic Park was released in year 1993
Starship Troopers was released in year 1997
The Matrix was released in year 1999Code language: plaintext (plaintext)

I’ll now show how to add to the list of tuples and how to sort it.

Add to the list of tuples

There’s two ways to add tuples to a list:

  • Use the list initializer syntax to combine creating the list and adding tuples to it.
  • Call Add().

Here’s an example of both ways to add a tuple to a list:

var movies = new List<(string title, int year)>()
{
    ("The Matrix", 1999) //add with list initializer syntax
};

movies.Add(("Jurassic Park", 1993));

Code language: C# (cs)

When you add a tuple to a list, it needs to have the same field types as the list’s tuple field types – (string, int) in this example.

You can specify the tuple field names when adding it to the list if you want, but it’s not required:

movies.Add((title: "Starship Troopers", year: 1997));
Code language: C# (cs)

Sort the list of tuples

The simplest way to sort a list of tuples is with OrderBy() (Linq). This allows you to pick which tuple fields to sort by in a straightforward manner. Here’s an example:

using System.Linq;

var movies = new List<(string title, int year)>()
{
    ("Starship Troopers", 1997),
    ("The Matrix", 1999),
    ("Jurassic Park", 1993)
};

movies = movies.OrderBy(t => t.year).ToList();

foreach(var movie in movies)
{
    Console.WriteLine($"{movie.title} ({movie.year})");
}
Code language: C# (cs)

Note: Use OrderByDescending() to sort in descending order.

This outputs the tuples sorted by year in ascending order:

Jurassic Park (1993)
Starship Troopers (1997)
The Matrix (1999)Code language: plaintext (plaintext)

In-place sort with List.Sort()

OrderBy() (Linq) returns a new list with the tuples in sorted order. If you want to do an in-place sort instead, use List.Sort(). You can pass in a comparison lambda to pick which fields to sort by. Here’s an example:

var movies = new List<(string title, int year)>()
{
    ("Starship Troopers", 1997),
    ("The Matrix", 1999),
    ("Jurassic Park", 1993)
};
movies.Sort((a, b) => a.year.CompareTo(b.year));

foreach(var movie in movies)
{
    Console.WriteLine($"{movie.title} ({movie.year})");
}
Code language: C# (cs)

This sorts the tuples by comparing the year field and outputs the following:

Jurassic Park (1993)
Starship Troopers (1997)
The Matrix (1999)Code language: plaintext (plaintext)

If you don’t pass in any parameters to List.Sort(), it uses ValueTuple.CompareTo(). This is pretty good. It sorts by all fields in the order they were declared. This is a good option if you want to sort by ALL fields.