Array.sort sorts numbers as strings
Without a comparator, sort() compares numbers as text, so 100 lands before 9. Pass (a, b) => a - b and copy the array first.
You sorted a list of scores and the 100 landed between the 1 and the 9. The array has numbers in it, so the order looks wrong for no reason.
const scores = [10, 9, 1, 100]
console.log(scores.sort())
// [ 1, 10, 100, 9 ]With no comparator, sort() turns each element into a string and compares the strings by their UTF-16 code units, per MDN. The string "10" starts with "1", which comes before "9", so 10 sorts before 9.
Pass a comparator
A comparator takes two elements, a and b, and returns a number. Negative means a goes first, positive means b goes first, and zero means they are equal.
const scores = [10, 9, 1, 100]
const ascending = [...scores].sort((a, b) => a - b)
const descending = [...scores].sort((a, b) => b - a)
console.log(ascending) // [ 1, 9, 10, 100 ]
console.log(descending) // [ 100, 10, 9, 1 ]
console.log(scores) // [ 10, 9, 1, 100 ]Subtracting gives a negative number when a is smaller, so a - b sorts up and b - a sorts down. The same shape sorts objects by a field: (x, y) => x.price - y.price puts the cheapest item first. The spread copy in the first two sort calls matters, and the last console.log shows why.
It sorts in place
sort changes the array you call it on and returns that same array. In the trap above, scores itself was reordered, and so is every other variable pointing at it.
If the array is React state or a prop, copy it first with [...scores] as in the second block. A sorted state array keeps the same reference, so a check by reference sees no change.
A boolean is not a comparator
It is tempting to write (a, b) => a > b. That returns true or false, which become 1 and 0. The function never returns a negative number, so it never says that a should go first.
const scores = [3, 1, 2, 5, 4, 9, 7, 8, 6, 10, 12, 11]
console.log([...scores].sort((a, b) => a > b))
// [3,1,2,5,4,9,7,8,6,10,12,11]
console.log([...scores].sort((a, b) => a - b))
// [1,2,3,4,5,6,7,8,9,10,11,12]V8 leaves the first result in its original order. A boolean comparator violates the consistency rules, and other engines can give a different order.