Back
Close

Using C# LINQ - A Practical Overview

player_one
1637.7K views

Methods - Changing element order

These LINQ methods reorder the elements in an IEnumerable<T> sequence. Each method in this chapter provides a different way of specifying the desired element order.

NOTE: Same comment as in the previous chapter. If I state that a method "returns a sequence", this is not technically true. The methods in this chapter return an IEnumerable<T>, which is a generator (or an iterator) that can provide a sequence on demand.

Reverse() method

The Reverse() method returns a new sequence that contains all the elements from the source sequence in the opposite order.

IEnumerable<string> strings = new List<string> { "first", "then", "and then", "finally" };
// Will contain { "finally", "and then", "then", "first" }
IEnumerable<string> result = strings.Reverse();

NOTE: The Reverse() LINQ method (an extension method to IEnumerable<T>) behaves differently from the Reverse() method on List<T>. This unfortunate situation can cause confusion. More on this in the Advanced Topics course.

OrderBy(<keySelector>) method

OrderBy() sorts the elements in the source sequence based on a key value. The key for an element is calculated by a keySelector delegate method passed into the OrderBy() call. The examples below demonstrate how this works.

List<string> strings = new List<string> { "first", "then", "and then", "finally" };
// Sort the strings by their length
// Will contain { "then", "first", "finally", "and then" }
IEnumerable<string> result = strings.OrderBy(str => str.Length);

// Sort the strings by the 3rd character
// Will contain { "and then", "then", "finally", "first" }
IEnumerable<string> result = strings.OrderBy(str => str[2]);

// Sort the strings by their reversed characters
// Will contain { "then", "and then", "first", "finally" }
IEnumerable<string> result = strings.OrderBy(str => new string(str.Reverse().ToArray()));

NOTE: The key values are sorted based on the output of the default comparer for the data type of the keys.

NOTE 2: There is also an OrderByDescending() version of this method that sorts in reverse order by the specified key value.

There is no Sort()

If you want to sort the elements within a sequence, then you will need to pass in an identity keySelector method that indicates that each element in the sequence is, itself, a key. Here is what that looks like:

List<string> strings = new List<string> { "first", "then", "and then", "finally" };
// Sort the strings in alphabetical order
// Will contain { "and then", "finally", "first", "then" }
IEnumerable<string> result = strings.OrderBy(str => str);

OrderBy() exercise

In the following exercise, try to sort all the input names by Last, in descending order.

OrderBy Exercise
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io