JavaScript Sorting Explained: Numbers, Strings, and Objects

Frontend developer who weaves creativity and code, book lover, avid traveller and a tech enthusiast
The JavaScript sort() method is one of those fundamental tools that every developer needs to master. At first glance, it seems simple enough - just call .sort() on an array and you're done, right?
Well, not quite! Let's dive deep into how this method really works and how to use it effectively.
π How sort () works by default ?
const fruits = ['banana', 'apple', 'cherry', 'date'];
fruits.sort();
console.log(fruits); // ['apple', 'banana', 'cherry', 'date']
Works fine if the data to be sorted is a plain array of strings.
But what if the data is an array of numbers, accented characters or strings with uppercase? We will look how to sort strings with mixed case and accented characters but first lets take a look at numbers.
[10, 2, 5, 1].sort();
// Output: [1, 10, 2, 5]
Surprised right? It is because default sorting converts everything to strings. But how is the output 1, 10, 2, 5 generated? Lets break this down.
When the above numbers are converted into strings ([10, 2, 5 , 1] β [β10β, β2β, β5β, β1β]), JavaScript compares character by character.
"1" < "2" < "5" β
but "10" starts with "1" β so it comes right after "1"
Hence, β‘οΈ output becomes:
[1, 10, 2, 5]
This is called lexicographical sorting (dictionary order). Just like sorting words "apple", "ball", "cat".
Lexicographic order = dictionary order
Meaning: compare character by character from left to right.
β Sorting numbers correctly
To sort numbers correctly, we need to use compare function.
arr.sort((a, b) => a - b);
For example:
const arr = [10, 2, 5, 1]
arr.sort((a, b) => a - b);
// Output => [1, 2, 5, 10]
π€·ββοΈ Why a - b works ?
| Return value | Meaning | Order |
| Negative value ( < 0 ) | a comes before b | a β b |
| Positive value ( > 0 ) | b comes before a | b β a |
| 0 | leave order unchanged | - |
π§ A simple trick to remember
Positive result β swap
Negative result β no swap
Zero β no need to change anything
[10, 2, 5].sort((a, b) => a - b); // output [2, 5, 10]
------------------------------------------------------
=> 10 - 2 = 8 β positive β swap
=> 2 - 5 = -3 β negative β donβt swap
=> 5 - 10 = -5 β negative β donβt swap
β By default, compare function sorts numbers into ascending order
[10, 2, 5, 1].sort((a, b) => a - b);
// Output => [1, 2, 5, 10]
β To sort into descending order, we can do
[10, 2, 5, 1].sort((a, b) => b - a);
// Output => [10, 5, 2, 1]
π€ localCompare() - a better way to sort strings
The localeCompare() method compares two strings in the current locale. To understand better, the locale refers to language + regional rules that your system/browser uses to compare and sort text.
A locale determines :
Alphabet order
How accented characters are treated
Case-sensitivity rules
Language-specific sorting differences etc.
Different languages have different sorting rules, even for the same characters. For example if we use defaul sort ()
["z", "Γ€", "a"].sort();
Output will be
["a", "z", "Γ€"] β incorrect in many languages
But using localCompare () has following advantages:
It respects language / region rules
["z", "Γ€", "a"].sort((a, b) => a.localeCompare(b));
In German (de)
["a", "Γ€", "z"] β correct
In Swedish (sv)
["a", "z", "Γ€"] β correct
Handles uppercase vs lowercase letters
["apple", "zoo", "Banana"].sort((a, b) => a.localeCompare(b)); // ["apple", "Banana", "zoo"] β correct alphabetical orderConsistent - always returns 1, 0 or -1
=> -1 β a comes before b ( a β b) => 1 β a comes after b (b β a) => 0 β they are equal
π· Sorting objects
const users = [
{ name: "Raj", age: 25 },
{ name: "Aman", age: 20 },
{ name: "Hari", age: 30 }
];
How do we sort the object based on name and age? We can do like this
// Based on age
users.sort((a, b) => a.age - b.age);
// Output => [{
age: 20,
name: "Aman"
}, {
age: 25,
name: "Raj"
}, {
age: 30,
name: "Hari"
}]
// Based on name
users.sort((a, b) => a.name.localeCompare(b.name))
// Output => [{
age: 20,
name: "Aman"
}, {
age: 30,
name: "Hari"
}, {
age: 25,
name: "Raj"
}]
π Conclusion
The sort() method is extremely powerful but tricky if you donβt understand how it works internally.
Just remember the simple rule:
Default sort = converts to string
For numbers / objects = always use compare function
Thank you for reading π.



