Utilizor
Contact Us

sort, reverse

Ordering array elements.

Sorting Arrays

The sort() method sorts an array alphabetically.

The reverse() method reverses the elements in an array.

By default, the sort() function sorts values as strings.

If numbers are sorted as strings, "25" is bigger than "100", because "2" is bigger than "1".

You can fix this by providing a compare function:

points.sort(function(a, b){return a - b});

Example

const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();