ZetCode

C# LINQ Order

last modified July 11, 2022

In this article we shows how to sort data in C# with LINQ queries.

Language-Integrated Query (LINQ) is a domain-specific language for querying data from various data sources, including arrays, lists, XML files, or databases.

We can use LINQ's Order, OrderDescending, OrderBy, and OrderByDescending methods to order data. The methods do not mutate the original sequence; they return a new sorted sequence.

In addition, we can use the orderby, ascending, and descending clauses with LINQ query expressions.

C# LINQ Order

The Order method sorts the elements of a sequence in ascending order.

Order<T>(IEnumerable<T>)
Order<T>(IEnumerable<T>, IComparer<T>)

The second method accepts an IComparer.

Program.cs
var words = new List<string> {
    "world", "War", "abbot", "Caesar", "castle", "sky", "den",
    "forest", "ocean", "water", "falcon", "owl", "rain", "Earth"
};

var sorted = words.Order().ToList();
sorted.ForEach(w => Console.WriteLine(w));

Console.WriteLine("-------------------------");

var sorted2 = words.Order(StringComparer.Ordinal).ToList();
sorted2.ForEach(w => Console.WriteLine(w));

We have a list of words. We sort the words in ascending order.

var sorted = words.Order().ToList();
sorted.ForEach(w => Console.WriteLine(w));

We sort the words with Order and print the sorted result to the console. The words are sorted in a case insensitive manner.

var sorted2 = words.Order(StringComparer.Ordinal).ToList();
sorted2.ForEach(w => Console.WriteLine(w));

To the overloaded method we also pass a string comparer. The StringComparer.Ordinal causes the words to be sorted in a case-sensitive manner.

$ dotnet run
abbot
Caesar
castle
den
Earth
falcon
forest
ocean
owl
rain
sky
War
water
world
-------------------------
Caesar
Earth
War
abbot
castle
den
falcon
forest
ocean
owl
rain
sky
water
world

C# LINQ OrderDescending

The OrderDescending method sorts the elements of a sequence in descending order.

Program.cs
var vals = new List<int> { 0, -2, 1, -3, 4, 3, 2, 5, 7, -1 }; 

var sorted = vals.OrderDescending();
Console.WriteLine(string.Join(", ", sorted));

In the program, we sort the integers in descending order.

$ dotnet run
7, 5, 4, 3, 2, 1, 0, -1, -2, -3

C# LINQ OrderBy

The OrderBy method sorts the elements of a sequence in ascending order according to a key.

OrderBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>)

The method accepts a keyselector function, which extracts a key from an element.

Program.cs
var users = new List<User>
{
    new ("John", "Doe", 1230),
    new ("Lucy", "Novak", 670),
    new ("Ben", "Walter", 2050),
    new ("Robin", "Brown", 2300),
    new ("Amy", "Doe", 1250),
    new ("Joe", "Draker", 1190),
    new ("Janet", "Doe", 980),
    new ("Albert", "Novak", 1930),
};

Console.WriteLine("sort ascending by last name and salary");

var sortedUsers = users.OrderBy(u => u.LastName).ThenBy(u => u.Salary);

foreach (var user in sortedUsers)
{
    Console.WriteLine(user);
}

record User(string FirstName, string LastName, int Salary);

In the example, we sort the users first by their last names, then by their salaries.

var sortedUsers = users.OrderBy(u => u.LastName).ThenBy(u => u.Salary);

We sort the users by their last names and then by their salaries in ascending order. The keyselector function in the form of a lambda extracts the LastName attribute to be the sort key.

$ dotnet run
sort ascending by last name and salary
User { FirstName = Robin, LastName = Brown, Salary = 2300 }
User { FirstName = Janet, LastName = Doe, Salary = 980 }
User { FirstName = John, LastName = Doe, Salary = 1230 }
User { FirstName = Amy, LastName = Doe, Salary = 1250 }
User { FirstName = Joe, LastName = Draker, Salary = 1190 }
User { FirstName = Lucy, LastName = Novak, Salary = 670 }
User { FirstName = Albert, LastName = Novak, Salary = 1930 }
User { FirstName = Ben, LastName = Walter, Salary = 2050 }

C# LINQ OrderByDescending

The OrderByDescending method sorts the elements of a sequence in descending order.

Program.cs
var users = new List<User>
{
    new ("John", "Doe", 1230),
    new ("Lucy", "Novak", 670),
    new ("Ben", "Walter", 2050),
    new ("Robin", "Brown", 2300),
    new ("Amy", "Doe", 1250),
    new ("Joe", "Draker", 1190),
    new ("Janet", "Doe", 980),
    new ("Albert", "Novak", 1930),
};

Console.WriteLine("sort descending by last name and salary");

var sortedUsers = users.OrderByDescending(u => u.LastName)
    .ThenByDescending(u => u.Salary);

foreach (var user in sortedUsers)
{
    Console.WriteLine(user);
}

record User(string FirstName, string LastName, int Salary);

In the program, we sort the users by last name and then by salary in descending order.

$ dotnet run
sort descending by last name and salary
User { FirstName = Ben, LastName = Walter, Salary = 2050 }
User { FirstName = Albert, LastName = Novak, Salary = 1930 }
User { FirstName = Lucy, LastName = Novak, Salary = 670 }
User { FirstName = Joe, LastName = Draker, Salary = 1190 }
User { FirstName = Amy, LastName = Doe, Salary = 1250 }
User { FirstName = John, LastName = Doe, Salary = 1230 }
User { FirstName = Janet, LastName = Doe, Salary = 980 }
User { FirstName = Robin, LastName = Brown, Salary = 2300 }

Ordering with query expressions

The next example uses the LINQ query expressions to sort data.

Program.cs
var words = new List<string> {
    "world", "War", "abbot", "Caesar", "castle", "sky", "den",
    "forest", "ocean", "water", "falcon", "owl", "rain", "Earth"
};

var sorted = from word in words
             orderby word
             select word;

Console.WriteLine(string.Join(", ", sorted));

var sorted2 = from word in words
             orderby word descending
             select word;

Console.WriteLine(string.Join(", ", sorted2));

We sort a list of words with orderby and descending clauses.

$ dotnet run
abbot, Caesar, castle, den, Earth, falcon, forest, ocean, owl, rain, sky, War, water, world
world, water, War, sky, rain, owl, ocean, forest, falcon, Earth, den, castle, Caesar, abbot

In this article we have ordered data in C# with LINQ.

List all C# tutorials.