The sort() method reorders array elements in place. By default it sorts as strings—which surprises beginners with numbers. This tutorial covers compare functions, ascending/descending order, sorting objects, and safe copy-first patterns.
01
Syntax
sort(compareFn?)
02
Default
String order
03
Numbers
(a,b)=>a-b
04
Mutates
Same array
05
Objects
By property
06
ES5+
Universal
Fundamentals
Introduction
Need names A–Z, prices low-to-high, or scores on a leaderboard? sort() rearranges elements inside the array. It returns the same array reference (now reordered), so you can chain or assign in one expression.
The catch: without a compare function, JavaScript converts each value to a string before comparing. That works for words but often breaks for multi-digit numbers.
Concept
Understanding the sort() Method
array.sort(compareFn?) sorts elements in place using UTF-16 string order when no function is provided. With a compare function, you control ordering: return negative if a comes first, zero if equal, positive if b comes first.
Modern JavaScript requires a stable sort: equal elements keep their relative order from before the sort.
💡
Beginner Tip
For numbers, memorize one line: (a, b) => a - b for ascending. Flip to b - a for descending. For strings, use localeCompare: (a, b) => a.localeCompare(b).
Compare a field on each object. Same numeric pattern: a.price - b.price. For strings, use a.name.localeCompare(b.name).
Applications
🚀 Common Use Cases
Leaderboards — scores high-to-low.
Product lists — sort by price or rating.
Names — alphabetical user directories.
Dates — compare timestamps or Date objects.
Table UI — column header click sorting.
Immutable display — [...data].sort(fn) for React state.
🧠 How sort() Runs
1
Receive compare fn
None? Use string conversion order.
Setup
2
Compare pairs
Engine picks a sort algorithm (stable in modern JS).
Compare
3
Reorder in place
Elements move inside the same array.
Mutate
4
✅
Return array
Same reference, now sorted.
Important
📝 Notes
Mutates the original array.
Default sort = string UTF-16 order, not numeric.
Returns the sorted array (same reference).
Stable sort since ES2019 (equal items keep relative order).
For immutability: [...arr].sort(fn) or toSorted(fn).
Undefined and holes behave per spec—avoid sparse arrays when sorting.
Do not assume a specific algorithm (TimSort-like internally in V8).
Compatibility
Browser & Runtime Support
Array.prototype.sort() is one of the oldest array methods—available since JavaScript 1.2. All browsers support it. Modern engines implement a stable sort (ES2019).
✓ Baseline · Universal
Array.prototype.sort()
Supported everywhere: Chrome, Firefox, Safari, Edge, IE, and all Node.js versions. Non-mutating toSorted() requires ES2023 (2023+ browsers).
100%Universal support
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
Array.sort()Excellent
Bottom line: Safe in every environment. Use a compare function for numbers; default string sort is the main beginner trap.
Wrap Up
Conclusion
sort() reorders arrays in place. Default string sorting is fine for words but misleading for numbers—use (a, b) => a - b instead. Copy first when you must preserve the original order.
Next, learn splice() to add, remove, or replace elements at specific indices.
Copy with spread before sorting if immutability matters
Keep compare functions pure (no side effects)
Sort objects by one clear field at a time
❌ Don’t
Rely on default sort() for multi-digit numbers
Forget that the original array is changed
Return non-numbers from compare (coerce to number)
Sort and assume order without storing the result
Confuse sort with reverse (flip vs reorder)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Array.sort()
Reorder elements with optional compare logic.
5
Core concepts
📝01
Syntax
sort(fn?)
API
🔢02
Default
String order.
Trap
💲03
Numbers
a - b.
Compare
🔄04
Mutates
In place.
Side effect
📦05
Objects
By property.
Real world
❓ Frequently Asked Questions
sort() reorders the elements of an array in place and returns the same array reference. Without a compare function, elements are converted to strings and sorted by UTF-16 code unit values.
Yes. sort() changes the original array. If you need the original order preserved, copy first with spread ([...arr].sort()) or use the non-mutating toSorted() method (ES2023).
Default sort compares string versions: '100' comes before '40' because '1' < '4'. Always pass a compare function for numeric sorting: (a, b) => a - b.
Return a negative number if a should come before b, zero if equal order, positive if a should come after b. For numbers ascending: (a, b) => a - b. Descending: (a, b) => b - a.
reverse() only flips the current order end-to-end. sort() reorders elements by a comparison rule (default string order or your compare function).
sort() has been available since ES1 and works in all browsers including very old environments. Modern engines use a stable sort algorithm (ES2019 requirement).
Did you know?
Before ES2019, some JavaScript engines used unstable sorts—equal elements could swap order unpredictably. Today’s spec requires stability, so ties preserve their original relative order.