Javascript - Array.sort() Does not Sort Number Correctly with Example

Sovary May 7, 2022 363
2 minutes read

In another programming language, we probably use built-in function or method to sort data in ascending or descending mostly it works fine. Weirdly in Javascript, the built-in sort function seems can not sort numberic properly.

Let's find out the below

let numbers = [7,2,1,17,4,10,5,11];
numbers.sort();
console.log(numbers);
//will print [1, 10, 11, 17, 2, 4, 5, 7]

In the above code, the number seems not to be sorted in ascending Why does array.sort() method, doesn’t sort the numbers as expected?

Sorting array in Javascript has two built-in functions

  • Array.sort()
  • Array.sort(comparer)

1. Array.sort() with no argument

By default, sort() function sorts values as strings. Exactly we don't input the value with single or double quote to define as string value but the function will treat it as string and sort alphabetically.

let fruit = ['banana', 'apple', 'mango'];
fruit.sort();
console.log(fruit); 

//will print => ['apple', 'banana', 'mango']

The fruit will print in order ascending ['apple', 'banana', 'mango']. Unfortunately, using the same method on numbers will display the same alphabetical sorting:

let numbers = [7,2,1,17,4];
numbers.sort();
console.log(numbers);
//will print [1,17,2,4,7]

The function returns an array [1,17,2,4,7] of integers ordered alphabetically rather than their value.

2. Array.sort([comparer]) with argument

The sort function with an argument to define the alternative sort order. Using this function we can manage how elements are ordered. We call the argument as comparer. The comparer should return a negative, zero, or positive value, depending on what we want to order in ascending or descending.

If comparator(a, b) returns:

  • If the result is negative a is sorted before b.
  • If the result is positive b is sorted before a.
  • If the result is 0 not change position at all between these two values.

So this option is compatible for us to sort numbers in Javascript. Let's see the below example

let numbers = [7,2,1,17,4];
numbers.sort(function(a,b)
{
  if (a < b) {
    return -1;
  }
  if (a > b) {
    return 1;
  }
  return 0;
}); 
console.log(numbers);

//print [1,2,4,7,11]

We can sort descending or ascending via this explicit function. We can use the short form with ES6 (arrow function) will produce the same result

let numbers = [7,2,1,17,4]
numbers.sort((a, b) => a - b)
console.log(numbers);
//print [1,2,4,7,17]

In case, you want to sort in descending just swap the variables

let numbers = [7,2,1,17,4]
//swap variable to get different return value
numbers.sort((a, b) => b - a)
console.log(numbers);
//print [17,7,4,2,1]

You might Also Like:

Javascript 
Author

Founder of CamboTutorial.com, I am happy to share my knowledge related to programming that can help other people. I love write tutorial related to PHP, Laravel, Python, Java, Android Developement, all published post are make simple and easy to understand for beginner. Follow him