Sorting Operators

Sorting operations use a single or multiple attributes to order elements of a sequence.

The first criterion executes a primary sort, and through a second criterion, elements are sorted within primary sort groups. The query operator methods used to perform sort operations follow: OrderBy, OrderByDescending, ThenBy, ThenByDescending, and Reverse.

The OrderBy method sorts values in ascending order based on a key. It uses the following syntax:

public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(
	this IEnumerable<TSource> source,
	Func<TSource, TKey> keySelector
)

Review two examples of its use below:

string[] terms = { "must", "be", "something", "in", "the", "water" };

IEnumerable<string> query = from term in terms
							   orderby term.Length
							   select term;

foreach (string s in query)
	Console.WriteLine(s);

class Guitar
{
	public string Brand { get; set; }
	public int Price { get; set; }
}

public static void OrderByOne()
{
	Guitar[] guitars = { new Guitar { Brand="Fender", Price=300 },
						new Guitar { Brand="Ibanez", Price=150 },
						new Guitar { Brand="Epiphone", Price=1200 } };
	IEnumerable<Guitar> query = guitars.OrderBy(guitar => guitar.Price);

	foreach (Guitar guitar in query)
	{
		Console.WriteLine("{0} - {1}", guitar.Brand, guitar.Price);
	}
}

The OrderByDescending method sorts values in descending order. It uses the following syntax:

public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey>(
	 this IEnumerable<TSource> source,
	 Func<TSource, TKey> keySelector
)

Review an example of its use below:

string[] terms = { "joy", "in", "repetition", "truly", "believing" };

IEnumerable<string> query = from term in terms
							   orderby term.Substring(0, 1) descending
							   select term;

foreach (string q in query)
	Console.WriteLine(q);

The ThenBy method executes a secondary sort in ascending order. It uses the following syntax:

public static IOrderedEnumerable<TSource> ThenBy<TSource, TKey>(
	this IOrderedEnumerable<TSource> source,
	Func<TSource, TKey> keySelector
)

Review an example of its use below:

string[] girls = { "Mayte", "Susan", "Nona", "Denise",
									"Patrice", "Kim", "Manuela", "Sheila" };
				
// Sort by length and then alphabetically
IEnumerable<string> query =
	girls.OrderBy(girl => girl.Length).ThenBy(girl => girl);

foreach (string girl in query)
{
	Console.WriteLine(girl);
}

The ThenByDescending method executes a secondary sort in descending order. It uses the following syntax:

public static IOrderedEnumerable<TSource> ThenByDescending<TSource, TKey>(
	  this IOrderedEnumerable<TSource> source,
	  Func<TSource, TKey> keySelector
)

The Reverse method reverses the element order of a collection. It uses the following syntax:

public static IEnumerable<TSource> Reverse<TSource>(
	this IEnumerable<TSource> source
)

Review an example below:

char[] Rogers = { 'R', 'o', 'g', 'e', 'r', 's'};

char[] reversed = Rogers.Reverse().ToArray();

foreach (char c in reversed)
{
	Console.Write(c + " ");
}

Console.WriteLine();