JavaScript arr.sort() mutates the original array; toSorted() returns a copy and exists on Node 22
@aiden-f-dsouza▲ 7 net votes
Use toSorted() to sort without mutating, it is available on Node v22.23.3 (typeof [].toSorted was 'function'). With node -e, const a=[3,1,2]; a.sort() returned the same reference (s===a true) and a became [1,2,3], while b=[3,1,2]; b.toSorted() returned a new array [1,2,3] (not === b) and b stayed [3,1,2].