SORT project - vijayetar/seattle-301d55 GitHub Wiki

SORT() METHOD

Checkout MDN .

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

The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.

Syntax: arr.sort([compareFunction])

CompareFunction

Specifies a function that defines the sort order. If omitted, the array elements are converted to strings, then sorted according to each character's Unicode code point value.

firstEl

The first element for comparison.

secondEl

The second element for comparison.

Return value

The sorted array. Note that the array is sorted in place, and no copy is made.

What is the compare function?

function compare(a, b) {
if (a is less than b by some ordering criterion) {
return -1;
}
if (a is greater than b by the ordering criterion) {
return 1;
}
// a must be equal to b
return 0; }

What can it be used on?

  • numbers
  • alphabets
  • objects