JavaScript Notes
Master JavaScript sort() method: compare function, sort numbers, strings, objects, toSorted(), stable sort, custom comparators, real-world examples, and interview Q&A.
sort() reorders the elements of an array in place. By default it converts elements to strings — which breaks numeric sorting. Mastering the compare function is the key to sorting anything correctly.
Sorting Numbers
Wrong: [10, 100, 2, 25, 40, 9] Ascending: [2, 9, 10, 25, 40, 100] Descending: [100, 40, 25, 10, 9, 2]
Sorting Strings
["Banana", "Cherry", "Date", "apple", "elderberry"] ["apple", "Banana", "Cherry", "Date", "elderberry"] ["cafe", "naïve", "résumé", "zebra"]
Sorting Arrays of Objects
["Meera: 73", "Priya: 88", "Rohit: 88", "Arjun: 95"] ["Arjun: 95", "Priya: 88", "Rohit: 88", "Meera: 73"]
Sorting by Date
2026-01-20: Review 2026-03-15: Launch 2026-05-01: Release
sort() vs toSorted() (ES2023)
sort() mutates the original. toSorted() returns a new sorted array:
Original after sort: [1, 1, 3, 4, 5, 9] Original after toSorted: [3, 1, 4, 1, 5, 9] Sorted copy: [1, 1, 3, 4, 5, 9]
Stable Sort (Guaranteed in ES2019+)
Since ES2019, JavaScript's sort() is guaranteed to be stable — elements with equal sort keys maintain their original relative order:
["B(1)", "D(1)", "A(2)", "C(2)"]
Real World Use Cases
["Mouse: ₹500", "Monitor: ₹15000", "Laptop: ₹80000"] 1. Carol: 5000 pts 2. Bob: 4200 pts 3. Alice: 4200 pts ["CSS", "HTML", "javascript", "nodejs", "React"]
Common Mistakes
| ❌ Wrong | ✅ Correct | Why |
|---|---|---|
arr.sort() on numbers | arr.sort((a, b) => a - b) | Default sort is lexicographic — 100 < 9 as strings |
arr.sort() and using original | [...arr].sort(...) or arr.toSorted(...) | sort() mutates the original |
(a, b) => a > b as compareFn | (a, b) => a > b ? 1 : a < b ? -1 : 0 or just a - b for numbers | compareFn must return number, not boolean |
| Forgetting case insensitivity | .sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase())) | Default string sort is case-sensitive |
Comparison: sort() vs toSorted()
| Feature | sort() | toSorted() (ES2023) |
|---|---|---|
| Mutates original | ✅ Yes | ❌ No |
| Returns | Same (mutated) array | New sorted array |
| Support | All browsers | Modern browsers / Node 20+ |
| Use when | You're OK mutating, or already have a copy | You want to preserve original |
Cheat Sheet
Interview Questions
Q1. What is the default behavior of sort() without a compare function?
It converts all elements to strings and sorts them lexicographically (alphabetically). This causes numeric sorting to fail — [10, 9, 2] becomes [10, 2, 9] because "10" < "2" alphabetically.
Q2. How do you sort an array of numbers correctly?
Provide a compare function: arr.sort((a, b) => a - b) for ascending, (a, b) => b - a for descending.
Q3. Does sort() mutate the original array?
Yes. sort() is an in-place sort — it modifies the original array and returns it. Use [...arr].sort(fn) or arr.toSorted(fn) (ES2023) to sort without mutation.
Q4. How do you sort an array of objects by a property?
Q5. What is a stable sort and is JavaScript's sort stable?
A stable sort preserves the relative order of elements that compare as equal. Since ES2019, JavaScript's sort() is guaranteed to be stable in all modern engines.
Q6. What should a compare function return?
A negative number if a should come before b, zero if order doesn't matter, positive number if b should come before a. Never return a boolean.
Q7. What is toSorted() and how is it different from sort()?
toSorted() (ES2023) returns a new sorted array without modifying the original. It's the immutable/functional alternative to sort().
Key Takeaways
- Default
sort()is lexicographic — always provide a compare function for numbers sort()mutates the original array — use[...arr].sort()ortoSorted()to avoid this- Compare function returns: negative →
afirst, zero → tie, positive →bfirst (a, b) => a - b= ascending,(a, b) => b - a= descending- JavaScript's sort is stable since ES2019
- Use
localeCompare()for correct string/locale-aware sorting
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for JavaScript Array sort() Method - Complete Tutorial with Examples.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, arrays, sort, javascript array sort() method - complete tutorial with examples
Related JavaScript Master Course Topics