Skip to content
Justice Tech School

Sorting and Filtering Lists

Copy before sorting because sort mutates. Filter and sort in render, not in state.

Example
const people = [{ n: "Zed", a: 30 }, { n: "Ada", a: 20 }];
const sorted = [...people].sort((x, y) => x.a - y.a);
const adults = people.filter((p) => p.a >= 25);
console.log(sorted.map((p) => p.n), adults.map((p) => p.n));

Result

[ 'Ada', 'Zed' ] [ 'Zed' ]

Exercise

Copy an array.

[arr]

Quiz Test

Why copy before sort?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.