How to use JavaScript Array.prototype.sort() - JavaScript Sort Explained with Examples

The sort() method sorts the elements of an array in place and returns the sorted array.

Syntax

arr.sort([compareFunction])

Parameters

Parameter Description Necessity
compareFunction Optional. The name of the function used to determine the order of the elements. If omitted, the elements are sorted in ascending, Unicode character order. Optional

Returns

The sorted array.

Remarks

The sort method sorts the Array object in place; no new Array object is created during execution.

If you supply a function in the compareFunction argument, it must return one of the following values:

  • A negative value if the first argument passed is less than the second argument.

  • Zero if the two arguments are equivalent.

  • A positive value if the first argument is greater than the second argument.

Examples

var fruit = ['cherries', 'apples', 'bananas'];
fruit.sort(); // ['apples', 'bananas', 'cherries']

var scores = [1, 10, 2, 21]; 
scores.sort(); // [1, 10, 2, 21]
// Watch out that 10 comes before 2,
// because '10' comes before '2' in Unicode code point order.

var things = ['word', 'Word', '1 Word', '2 Words'];
things.sort(); // ['1 Word', '2 Words', 'Word', 'word']
// In Unicode, numbers come before upper case letters,
// which come before lower case letters.
var a = new Array(4, 11, 2, 10, 3, 1);

var b = a.sort();
document.write(b);
document.write("<br/>");

// This is ASCII character order.
// Output: 1,10,11,2,3,4)

// Sort the array elements with a function that compares array elements.
b = a.sort(CompareForSort);
document.write(b);
document.write("<br/>");
// Output: 1,2,3,4,10,11.

// Sorts array elements in ascending order numerically.
function CompareForSort(first, second)
{
    if (first == second)
        return 0;
    if (first < second)
        return -1;
    else
        return 1; 
}

Array.prototype.sort

This method sorts the elements of an array in place and returns the array.

The sort() method follows the ASCII order !

index character
33 !
34 "
35 #
36 $
37 %
var myArray = ['#', '!'];
var sortedArray = myArray.sort();   // ['!', '#'] because in the ASCII table "!" is before "#"

myArray = ['a', 'c', 'b'];
console.log(myArray.sort()); // ['a', 'b', 'c']
console.log(myArray) // ['a', 'b', 'c']

myArray = ['b', 'a', 'aa'];
console.log(myArray.sort());   // ['a', 'aa', 'b']

myArray = [1, 2, 13, 23];
console.log(myArray.sort());   // [1, 13, 2, 23] numbers are treated like strings!

Advanced usage

The sort() method can also accept a parameter: array.sort(compareFunction)

For example

function compare(a, b){
  if (a < b){return -1;}
  if (a > b){return 1;}
  if (a === b){return 0;}
}

var myArray = [1, 2, 23, 13];
console.log(myArray.sort()); // [ 1, 13, 2, 23 ]
console.log(myArray.sort(compare));   // [ 1, 2, 13, 23 ]

myArray = [3, 4, 1, 2];
sortedArray = myArray.sort(function(a, b){.....});   // it depends from the compareFunction
17 Likes