How to Sort an Array in JavaScript
Use sort() when you need to reorder array items alphabetically, numerically, or by object properties. The key is choosing the right compare function so the result matches the order you actually want.
What you’ll build or solve
You’ll learn how to sort an array in JavaScript using sort(). You’ll also know how to sort strings, numbers, and arrays of objects safely.
Learn JavaScript on Mimo
When this approach works best
This approach is the right choice when the same data needs to be displayed in a predictable order.
Common real-world scenarios include:
- Product price sorting
- Alphabetical name lists
- Leaderboards
- Date ordering
- Priority task views
This is a bad idea when the original array order must stay untouched. In that case, copy the array before sorting.
Prerequisites
You only need:
- A JavaScript file or browser console
- A basic array
- Basic JavaScript array knowledge
Step-by-step instructions
Step 1: Use sort() with the right compare function
For strings, sort() works directly.
JavaScript
const names = ["Mia", "Alex", "Jordan"];
names.sort();
console.log(names);
For numbers, always add a compare function.
JavaScript
const scores = [45, 72, 8, 100];
scores.sort((a, b) => a - b);
console.log(scores);
For descending order, flip the comparison.
JavaScript
scores.sort((a, b) => b - a);
What to look for:
- String sorting works by default
- Number sorting needs a compare function
a - bsorts ascendingb - asorts descendingsort()changes the original array
Examples you can copy
Alphabetical names
JavaScript
const students = ["Taylor", "Alex", "Morgan"];
students.sort();
Low to high prices
JavaScript
const prices = [29, 9, 49, 19];
prices.sort((a, b) => a - b);
Sort by object property
JavaScript
const users = [
{ name: "Alex", age: 30 },
{ name: "Mia", age: 24 }
];
users.sort((a, b) => a.age - b.age);
Common mistakes and how to fix them
Mistake 1: Sorting numbers without a compare function
What the reader might do:
JavaScript
const numbers = [100, 8, 20];
numbers.sort();
Why it breaks: JavaScript converts values to strings, so "100" comes before "8".
Corrected approach:
JavaScript
numbers.sort((a, b) => a - b);
Mistake 2: Expecting the original array to stay unchanged
What the reader might do:
JavaScript
scores.sort((a, b) => a - b);
console.log(scores);
Why it breaks: sort() mutates the original array.
Corrected approach:
JavaScript
const sortedScores = [...scores].sort((a, b) => a - b);
Mistake 3: Returning true or false in the compare function
What the reader might do:
JavaScript
numbers.sort((a, b) => a > b);
Why it breaks: the compare function should return a negative, zero, or positive number.
Corrected approach:
JavaScript
numbers.sort((a, b) => a - b);
Troubleshooting
If numbers sort strangely, add a compare function.
If the original order changes unexpectedly, copy the array first.
If object sorting fails, verify the property name inside the callback.
If descending order looks wrong, reverse the subtraction order.
Quick recap
- Use
sort()to reorder arrays - Strings sort directly
- Numbers need a compare function
sort()mutates the original array- Copy first if the original order matters
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot