A pipe that must not mutate in Angular

Angular interview exercise on Pipes & directives, difficulty 2 of 3. Solve it in the editor beside a worked example; the tests run the moment you do.

Write SortByPipe (name "sortBy"): transform(items, key, direction = "asc") returns a NEW array of the objects sorted by that key (numbers or strings), descending when direction is "desc". LeaderboardComponent lists `players` by score, highest first, as <li>name: score</li>.

Each exercise pairs a worked example with a twin problem to solve in a real editor; the tests run the moment you do. Progress is saved in your browser, and in your account if you sign in.

More Pipes & directives in Angular

All Angular exercises · Other languages and frameworks