0
0
C Sharp (C#)programming~20 mins

OrderBy and sorting in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
OrderBy Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this OrderBy example?
Consider the following C# code that sorts a list of integers using OrderBy. What will be printed to the console?
C Sharp (C#)
using System;
using System.Collections.Generic;
using System.Linq;

class Program {
    static void Main() {
        List<int> numbers = new() { 5, 3, 8, 1 };
        var sorted = numbers.OrderBy(n => n);
        foreach(var num in sorted) {
            Console.Write(num + " ");
        }
    }
}
A1 3 5 8
B8 5 3 1
C5 3 8 1
D3 5 1 8
Attempts:
2 left
💡 Hint
OrderBy sorts elements in ascending order by default.
Predict Output
intermediate
2:00remaining
What is the output when sorting strings by length?
This code sorts a list of strings by their length using OrderBy. What will be printed?
C Sharp (C#)
using System;
using System.Collections.Generic;
using System.Linq;

class Program {
    static void Main() {
        List<string> words = new() { "apple", "dog", "banana", "cat" };
        var sorted = words.OrderBy(w => w.Length);
        foreach(var w in sorted) {
            Console.Write(w + " ");
        }
    }
}
Acat dog apple banana
Bapple dog banana cat
Cbanana apple dog cat
Ddog cat apple banana
Attempts:
2 left
💡 Hint
OrderBy sorts by the key selector, here the string length.
Predict Output
advanced
2:00remaining
What is the output of this OrderByDescending with complex objects?
Given this code that sorts a list of Person objects by Age descending, what will be printed?
C Sharp (C#)
using System;
using System.Collections.Generic;
using System.Linq;

record Person(string Name, int Age);

class Program {
    static void Main() {
        List<Person> people = new() {
            new("Alice", 30),
            new("Bob", 25),
            new("Charlie", 35)
        };
        var sorted = people.OrderByDescending(p => p.Age);
        foreach(var p in sorted) {
            Console.Write(p.Name + " ");
        }
    }
}
ACharlie Bob Alice
BCharlie Alice Bob
CBob Alice Charlie
DAlice Bob Charlie
Attempts:
2 left
💡 Hint
OrderByDescending sorts from largest to smallest by the key.
Predict Output
advanced
2:00remaining
What is the output of this chained OrderBy and ThenBy?
This code sorts a list of tuples first by the first item ascending, then by the second item descending. What is printed?
C Sharp (C#)
using System;
using System.Collections.Generic;
using System.Linq;

class Program {
    static void Main() {
        var list = new List<(int, int)>{ (1, 2), (1, 3), (2, 1), (2, 2) };
        var sorted = list.OrderBy(x => x.Item1).ThenByDescending(x => x.Item2);
        foreach(var t in sorted) {
            Console.Write($"({t.Item1},{t.Item2}) ");
        }
    }
}
A(1,3) (1,2) (2,2) (2,1)
B(1,2) (1,3) (2,1) (2,2)
C(2,2) (2,1) (1,3) (1,2)
D(2,1) (2,2) (1,2) (1,3)
Attempts:
2 left
💡 Hint
OrderBy sorts by first item ascending, ThenByDescending sorts by second item descending within groups.
🧠 Conceptual
expert
2:00remaining
Which option causes a runtime error when sorting with OrderBy?
Given a list of objects, which code snippet will cause a runtime exception when using OrderBy in C#?
Alist.OrderBy(x => x.GetHashCode())
Blist.OrderBy(x => x.PropertyThatExists)
Clist.OrderBy(x => x.NonExistentProperty)
Dlist.OrderBy(x => x.ToString())
Attempts:
2 left
💡 Hint
Accessing a property that does not exist will cause an error at runtime.