Quiz di JavaScript: Sorting Gotchas

JavaScript's sort() has some famous surprises. This quiz tests the default string ordering, compare functions, in-place sorting vs toSorted(), stable sorts and the comparator bugs that pass a quick test but fail later.

Medio 10 domande ~6 minuti Superato con il 80% Node.js 22

Scegli una modalità

Stai giocando come ospite: accedi per salvare XP e serie.

Domanda di esempio

Domanda 1 di 10

What does this print?

const nums = [10, 1, 5, 100];
console.log(nums.sort().join(","));
Mostra la risposta

C. 1,10,100,5 Without a compare function, sort() converts items to strings and compares them character by character. '100' comes before '5' because '1' is less than '5'.

Cosa copre questo quiz

  • Why the default sort() compares items as strings
  • Writing compare functions like (a, b) => a - b
  • sort() changes the array, toSorted() returns a copy
  • Case, localeCompare() and undefined items
  • Sorting objects by a property, and stability

Domande frequenti

Serve un account?

No. Chiunque può giocare. Accedi per salvare XP, serie, badge e cronologia.

Posso rifarlo?

Sì. Il tuo miglior punteggio resta salvato, e domande e risposte vengono mescolate ogni volta.