This post explains a quiz originally shared as a LinkedIn poll.
πΉ The Question
const scores = [8, 80, 9, 100, 15];
scores.sort();
console.log(scores);
console.log(scores[0] < scores[4]);
Hint: Consider how sort() compares values when no comparator function is provided.
πΉ Solution
Correct Answer: B) [100, 15, 8, 80, 9] and false
The output is:
[100, 15, 8, 80, 9]false
π§ How this works
This is one of JavaScript's most counterintuitive behaviors. When you call Array.prototype.sort() without a comparator function, all elements are converted to strings and sorted lexicographically (dictionary order), not numerically.
Lexicographic comparison works character by character from left to right:
-
'100'comes before'15'because'1'equals'1', but'0'<'5' -
'8'comes before'80'and'9'because'8'<'9'in Unicode -
'80'comes before'9'because'8'<'9'
The sort produces [100, 15, 8, 80, 9] β completely wrong for numeric ordering!
For the second output: scores[0] is 100 and scores[4] is 9. The comparison 100 < 9 is a numeric comparison (both values are numbers), which correctly evaluates to false.
The irony: The "sorted" array has its largest element first and a smaller element last, yet scores[0] < scores[4] returns false because numeric comparison works correctly β it's only the sort that's broken.
π Line-by-line explanation
const scores = [8, 80, 9, 100, 15]β creates an array of numbers-
scores.sort()β sorts in place using string comparison:- Each number is converted to a string:
['8', '80', '9', '100', '15'] - Strings are compared character by character (Unicode code points)
-
'1'(code 49) <'8'(code 56) <'9'(code 57) - Result:
['100', '15', '8', '80', '9']β converted back:[100, 15, 8, 80, 9]
- Each number is converted to a string:
console.log(scores)β[100, 15, 8, 80, 9]console.log(scores[0] < scores[4])β100 < 9βfalse
The misleading part: Developers expect sort() to "just work" on numbers like it does in most other languages. JavaScript's design choice to stringify first makes sense for mixed-type arrays but creates this gotcha for numeric arrays.
πΉ The Fix
Always provide a comparator for numeric sorting:
// Ascending order
scores.sort((a, b) => a - b);
// Result: [8, 9, 15, 80, 100]
// Descending order
scores.sort((a, b) => b - a);
// Result: [100, 80, 15, 9, 8]
πΉ Key Takeaways
-
Array.sort()without arguments converts elements to strings and sorts lexicographically - This affects all non-string types: numbers, dates, booleans, objects (which become
'[object Object]') - Always provide a comparator function for numeric sorting:
(a, b) => a - b -
sort()mutates the original arrayβit doesn't return a new one - This is one of JavaScript's most common production bugs, especially when code works in testing with small sample data
Top comments (0)