Ordering Operators
OrderBy - Simple 1
This sample uses orderby to sort a list of words alphabetically.
Code:
public void Linq28()
{
string[] words = { "cherry", "apple", "blueberry" };
var sortedWords = words.OrderBy(word => word);
Log.WriteLine("The sorted list of words:");
foreach (var w in sortedWords)
{
Log.WriteLine(w);
}
}
Result:
OrderBy - Simple 2
This sample uses orderby to sort a list of words by length.
Code:
public void Linq29()
{
string[] words = { "cherry", "apple", "blueberry" };
var sortedWords = words.OrderBy(word => word.Length);
Log.WriteLine("The sorted list of words (by length):");
foreach (var w in sortedWords)
{
Log.WriteLine(w);
}
}
Result:
OrderBy - Simple 3
This sample uses orderby to sort a list of products by name. Use the "descending" keyword at the end of the clause to perform a reverse ordering.
Code:
public void Linq30()
{
List products = GetProductList();
var sortedProducts = products.OrderBy(prod => prod.ProductName);
ObjectDumper.Write(sortedProducts);
}
Result:
OrderBy - Comparer
This sample uses an OrderBy clause with a custom comparer to do a case-insensitive sort of the words in an array.
Code:
public void Linq31()
{
string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" };
var sortedWords = words.OrderBy(a => a, new CaseInsensitiveComparer());
ObjectDumper.Write(sortedWords);
}
Result:
OrderByDescending - Simple 1
This sample uses orderby and descending to sort a list of doubles from highest to lowest.
Code:
public void Linq32()
{
double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 };
var sortedDoubles = doubles.OrderByDescending(d => d);
Log.WriteLine("The doubles from highest to lowest:");
foreach (var d in sortedDoubles)
{
Log.WriteLine(d);
}
}
Result:
OrderByDescending - Simple 2
This sample uses orderby to sort a list of products by units in stock from highest to lowest.
Code:
public void Linq33()
{
List products = GetProductList();
var sortedProducts = products.OrderByDescending(prod => prod.UnitsInStock);
ObjectDumper.Write(sortedProducts);
}
Result:
OrderByDescending - Comparer
This sample uses method syntax to call OrderByDescending because it enables you to use a custom comparer.
Code:
public void Linq34()
{
string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" };
var sortedWords = words.OrderByDescending(a => a, new CaseInsensitiveComparer());
ObjectDumper.Write(sortedWords);
}
Result:
ThenBy - Simple
This sample uses a compound orderby to sort a list of digits, first by length of their name, and then alphabetically by the name itself.
Code:
public void Linq35()
{
string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
var sortedDigits = digits.OrderBy(digit => digit.Length).ThenBy(digit => digit);
Log.WriteLine("Sorted digits:");
foreach (var d in sortedDigits)
{
Log.WriteLine(d);
}
}
Result:
ThenBy - Comparer
The first query in this sample uses method syntax to call OrderBy and ThenBy with a custom comparer to sort first by word length and then by a case-insensitive sort of the words in an array. The second two queries show another way to perform the same task.
Code:
public void Linq36()
{
string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" };
var sortedWords =
words.OrderBy(a => a.Length)
.ThenBy(a => a, new CaseInsensitiveComparer());
// Another way.
var sortedWords2 = words.OrderBy(word => word.Length);
var sortedWords3 = sortedWords2.ThenBy(a => a, new CaseInsensitiveComparer());
ObjectDumper.Write(sortedWords);
ObjectDumper.Write(sortedWords3);
}
Result:
ThenByDescendin - Simple
This sample uses a compound orderby to sort a list of products, first by category, and then by unit price, from highest to lowest.
Code:
public void Linq37()
{
List products = GetProductList();
var sortedProducts = products.OrderBy(prod => prod.Category).ThenByDescending(prod => prod.UnitPrice);
ObjectDumper.Write(sortedProducts);
}
Result:
ThenByDescendin - Comparer
This sample uses an OrderBy and a ThenBy clause with a custom comparer to sort first by word length and then by a case-insensitive descending sort of the words in an array.
Code:
public void Linq38()
{
string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" };
var sortedWords = words.OrderBy(a => a.Length).ThenByDescending(a => a, new CaseInsensitiveComparer());
ObjectDumper.Write(sortedWords);
}
Result:
Reverse
This sample uses Reverse to create a list of all digits in the array whose second letter is 'i' that is reversed from the order in the original array.
Code:
public void Linq39()
{
string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
var reversedIDigits = (digits.Where(digit => digit[1] == 'i')).Reverse();
Log.WriteLine("A backwards list of the digits with a second character of 'i':");
foreach (var d in reversedIDigits)
{
Log.WriteLine(d);
}
}
Result: