๐ŸŽ“ A1Learner
๐Ÿ”
0 of 38 lessons completed

C# Filtering and Sorting

Filtering narrows a sequence down to the items that pass a test. Sorting then puts the kept items into a chosen order.

Chained Where calls

The first Where keeps numbers above 4. The second Where keeps only the even numbers from that set.

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        List<int> numbers = new List<int> { 5, 12, 8, 20, 3, 15 };
        var result = numbers.Where(n => n > 4).Where(n => n % 2 == 0);
        foreach (int n in result)
        {
            Console.WriteLine(n);
        }
    }
}

OrderBy sorts ascending

OrderBy reads each number as its own sort key. The result prints in rising order.

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        List<int> numbers = new List<int> { 7, 2, 9, 4 };
        var sorted = numbers.OrderBy(n => n);
        foreach (int n in sorted)
        {
            Console.WriteLine(n);
        }
    }
}

OrderByDescending reverses the order

OrderByDescending uses the same key in the opposite direction. The largest number prints first.

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        List<int> numbers = new List<int> { 3, 9, 1, 7 };
        var sorted = numbers.OrderByDescending(n => n);
        foreach (int n in sorted)
        {
            Console.WriteLine(n);
        }
    }
}

ThenBy breaks ties

OrderBy sorts the words by length. ThenBy sorts words of equal length in alphabetical order.

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        List<string> words = new List<string> { "pear", "fig", "apple", "kiwi", "plum" };
        var sorted = words.OrderBy(w => w.Length).ThenBy(w => w);
        foreach (string w in sorted)
        {
            Console.WriteLine(w);
        }
    }
}

Example

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        List<int> scores = new List<int> { 45, 12, 78, 34, 56 };
        var ranked = scores.Where(n => n > 30).OrderByDescending(n => n);
        foreach (int n in ranked)
        {
            Console.WriteLine(n);
        }
    }
}

Key points

  • Chained Where calls apply one test after another.
  • An item must pass every test in the chain to survive.
  • OrderBy sorts a sequence from the smallest key to the largest.
  • OrderByDescending sorts from the largest key to the smallest.
  • ThenBy adds a second sort key for items that tie on the first key.
  • ThenBy follows an OrderBy or OrderByDescending call.

In this example, Where keeps the scores above 30. OrderByDescending sorts those scores from largest to smallest. The loop prints 78, 56, 45 and 34.

Note

Sorting reads a key from each item and compares those keys.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What does ThenBy do in a sorting chain?