Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

Why can `sort()` put numbers in an unexpected order?

Quick answer

JavaScript’s default array sort() compares elements as strings, not as numbers, so values such as 100 can come before 25. Pass a numeric comparison function, such as (a, b) => a - b, for ascending numeric order. sort() also changes the array it sorts, so copy the array first if you need to preserve its original order. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have a JavaScript array of numbers, but sort() puts them in an order that looks wrong. Why?

Geez

With JavaScript’s Array.prototype.sort(), calling sort() without a comparison function sorts elements as strings, comparing their UTF-16 code units. That means the result follows text-like ordering rather than numeric size: for example, 100 can appear before 25. [S1]

For a small example, start with [25, 100, 3]. The default call, numbers.sort(), can produce [100, 25, 3]. Nothing is wrong with the values; the default comparison rule is simply not numeric.

Follow-up question

What should I pass to sort() to arrange them by numeric value?

Geez

Pass a function that compares the two numbers being considered. For ascending order, use (a, b) => a - b. The difference tells sort() which number should come first, giving the smallest-to-largest result. [S1]

Worked before-and-after example:

const numbers = [25, 100, 3];

numbers.sort();

Result after default sort: [100, 25, 3]

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

Result after numeric sort: [3, 25, 100]

Use the numeric comparator when the array contains numbers and the intended order is based on their numeric values. The example assumes the elements are ordinary numbers.

Follow-up question

Does that change the array I started with, or does sort() make a new one?

Geez

sort() reorders the array it is called on and returns that same array; it does not leave the original array’s order untouched. [S1] So after numbers.sort((a, b) => a - b), the array named numbers is now in ascending numeric order.

If you need to keep the starting order, make a copy and sort the copy. For example: const sortedNumbers = [...numbers].sort((a, b) => a - b);. Here, numbers remains the array to preserve, while sortedNumbers holds the sorted copy. This example assumes you have an array named numbers and want ascending order.

Follow-up question

Can you give me a quick way to choose the right call and check the result?

Geez

First decide whether you want to reorder the existing array or preserve it. Then use a numeric comparator for numeric order, and inspect the variable you actually intend to use afterward. For ascending order, the reusable pattern is values.sort((a, b) => a - b);. If the original order matters, use const sortedValues = [...values].sort((a, b) => a - b); instead.

For the example [25, 100, 3], check that the ascending result reads [3, 25, 100], not [100, 25, 3]. The documented behavior establishes how JavaScript array sort() works and shows a numeric comparator; it does not determine the intended order for your particular data. Choose ascending or another order based on what your task needs. [S1]

A practical checklist

  • Check whether your call supplies a comparison function.

  • For ascending numeric order, use (a, b) => a - b.

  • Decide whether sorting should reorder the existing array.

  • If the original order must remain, copy the array before sorting.

  • Check the resulting array against the order your task requires.

A few more useful answers

Why does `100` appear before `25` with plain `sort()`?

Without a comparison function, JavaScript converts array elements to strings and compares them as text-like values, not by numeric size. [S1]

Will assigning the result of `sort()` preserve my original array?

No. sort() changes the array it sorts and returns that same array. Sort a copy if you need to preserve the original order. [S1]

What comparator should I use for ascending numeric order?

Use (a, b) => a - b, as in values.sort((a, b) => a - b). [S1]

Sources

References used for this guide.

  1. [S1] Array.prototype.sort() — JavaScript | MDN
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work