Skip to main content

Practice · 2 of 3

A comparer for anything smaller

CHALLENGE
Difficulty: advanced+25 XP

Implement class Box { public int Weight; } and a contravariant comparer adapter: static IComparer<Box> ByWeight() => Comparer<Box>.Create((x, y) => x.Weight.CompareTo(y.Weight)); in class Solution. Then use it: static void Sort(List<Box> boxes) sorts by Weight ascending in place using the comparer.

Back to lesson: Practice: Generic machinery