JavaScript-Quiz: 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.

Mittel 10 Fragen ~6 Minuten Bestanden ab 80% Node.js 22

Modus wählen

Du spielst als Gast: Melde dich an, damit XP und Serie gespeichert werden.

Beispielfrage

Frage 1 von 10

What does this print?

const nums = [10, 1, 5, 100];
console.log(nums.sort().join(","));
Lösung zeigen

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'.

Darum geht es in diesem 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

Häufige Fragen

Brauche ich ein Konto?

Nein. Jeder kann spielen. Mit Anmeldung werden XP, Serie, Abzeichen und Verlauf gespeichert.

Kann ich es wiederholen?

Ja. Dein Bestwert bleibt erhalten, und Fragen und Antworten werden jedes Mal neu gemischt.