JavaScript Array sort() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

Array.prototype.sort(compareFn?) reorders elements in place and returns the same array. With no compare function, values become strings and sort by UTF-16 order — fine for text, wrong for most numbers. Need a non-mutating sort? Use toSorted() or copy first.

Remember
array.sort(compareFn?)

default  → string order (UTF-16)
numbers  → always pass (a, b) => a - b

compare returns:
  < 0  → a before b
    0  → keep relative order (stable)
  > 0  → a after b

mutates the array ✓
keep original? → arr.slice().sort(fn)

Syntax

Call array.sort. Start with this line.

JavaScript
array.sort(compareFn?)
CallWhat it does
array.sort(compareFn?)Primary signature.
array.sort(...)Call with the arguments your case needs.

Built into JavaScript. Call sort(...) on an array instance — no import required.

Library declarations
JavaScript
array.sort(compareFn?)

Parameters

ParameterDescription
ArrayRequired. Target that will be reordered in place
compareFnOptional. Controls order; required for correct numeric sort

Return Value

Return ValueDescription
*Same array reference, now sorted

JavaScript Examples

Example 1 — Sort Strings Alphabetically

Default sort() works well for plain lowercase strings.

JavaScript
const fruits = ["banana", "apple", "cherry"];

fruits.sort();

console.log(fruits);
// ["apple", "banana", "cherry"]
Try It Yourself

How It Works

  1. Call sort with no compare. Each value is treated as a string.
  2. UTF-16 order. Characters are compared by code units — "a" before "b".
  3. In place. fruits itself becomes ["apple", "banana", "cherry"].
  4. Locale tip. For real language sorting, prefer localeCompare.

Example 2 — Default Sort Pitfall With Numbers

Without a compare function, numbers sort like strings — often wrong.

JavaScript
const nums = [40, 5, 100, 9];

nums.sort();

console.log(nums);
// [100, 40, 5, 9]  ← not numeric order!
Try It Yourself

How It Works

  1. Values become strings. Compare "100", "40", "5", "9".
  2. First character wins. "1" < "4", so 100 comes before 40.
  3. Looks broken. This is expected default behavior — not a bug in sort.
  4. Fix next. Pass (a, b) => a - b for true numeric order.

Example 3 — Sort Numbers Ascending

Use subtraction in the compare function for correct numeric order.

JavaScript
const nums = [40, 5, 100, 9];

nums.sort((a, b) => a - b);

console.log(nums);
// [5, 9, 40, 100]
Try It Yourself

How It Works

  1. Compare by value. a - b is negative when a is smaller.
  2. Smaller first. Negative results place a before b.
  3. Ascending result. Order becomes [5, 9, 40, 100].
  4. Descending tip. Flip to (a, b) => b - a for high-to-low.

Browser & Runtime Support

Array.prototype.sort() has been part of JavaScript since the earliest editions. Modern engines provide a stable sort (ES2019).

✓ Baseline · Universal

Array.prototype.sort()

Supported in every major browser (including Internet Explorer) and all Node.js versions. No polyfill required.

100% Universal support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Array.sort() Excellent

Bottom line: Safe everywhere. For immutable sorting in modern engines, prefer toSorted() (ES2023) when you cannot mutate the source.

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. To keep the original order, copy first with slice or spread, 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, positive if a should come after b. Ascending numbers: (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 early JavaScript and works in all browsers. Modern engines use a stable sort (ES2019 requirement).

Next: splice()

Learn how to remove, replace, or insert elements at any index in place.

splice() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful