Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Function

sort

Array.prototype.sort() sorts an array in place and returns the same array. By default it compares string representations in UTF-16 order; pass a comparator for custom ordering.

How it works

sort() rearranges an array's existing elements in place and returns that same array. With no comparator, it compares string representations in UTF-16 code-unit order; with a comparator, its numeric result determines which element comes first.

Syntax

sort()
sort(compareFn)

Parameters

NameTypeDescription
compareFnfunctionOptional function receiving two elements and returning a number that determines their relative order.

Returns

The reference to the original array, now sorted.

Key points

  • The method rearranges the existing array and returns that identical array object.
  • Leaving out the comparator triggers string conversion before comparison.
  • Default ordering follows character code-point values rather than numeric magnitude.
  • Return a negative comparator result to place the first argument before the second.
  • Return a positive comparator result to place the first argument after the second.
  • A zero or NaN comparator result treats the compared elements as equal in ordering.

Common mistakes

  • Without a comparator, numeric-looking values are sorted lexicographically rather than by numeric value.
  • The comparator is not invoked for undefined elements, and those elements always move to the end.
  • Do not rely on a particular sorting algorithm or complexity because implementations may differ.
  • Use toSorted() instead when preserving the original array is required.

Example: Sort numbers numerically

const scores = [10, 2, 30];
scores.sort((a, b) => a - b);
console.log(scores); // [2, 10, 30]

Provide a numeric comparator when sorting numbers so their values determine ascending order.

When to use it

Numeric ascending order

Ordering numeric data such as scores, prices, or timestamps with (a, b) => a - b.

Sort objects by a property

Arranging records by a selected field with a comparator that compares the field values.

Locale-aware text order

Ordering text according to a locale by supplying a comparator such as one produced by Intl.Collator.

Interview questions

QDoes sort() mutate the original array?

sort() mutates the array and returns a reference to that same, now-sorted array. It does not create a separate array.

Follow-up: How would you sort without mutation? Use toSorted(), which returns a sorted copy while leaving the source array unchanged.

QWhat ordering does sort() use by default?

Without compareFn, elements are converted to strings and compared in UTF-16 code-unit order. Therefore, the default behavior is not numeric ordering for ordinary arrays.

Follow-up: How do you sort numbers ascending? Pass (a, b) => a - b as the comparator.

QHow does compareFn control ordering?

A comparator receives two defined elements, a and b. A negative result places a first, a positive result places b first, and zero or NaN treats them as equal.

Follow-up: What happens to undefined elements? They are placed at the end without calling the comparator for them.

  • Array.prototype.toSorted()
  • Array.prototype.reverse()
  • Intl.Collator

Source: Array.prototype.sort () - JavaScript | MDN. Checked 2026-10-15.