Table of Contents
The sort() method sorts an array alphabetically:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Array Sort</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Alphabetic Sort</h4>
<p id="demo"></p>
<script>
const fruits = ["Banana", "Orange", "Apple", "Mango"];
// Sort alphabetically
fruits.sort();
document.getElementById("demo").innerHTML =
"Original: Banana, Orange, Apple, Mango<br>" +
"After sort(): " + fruits;
</script>
</body>
</html>

Result View Example
The reverse() method reverses the elements in an array. You can reverse after sorting for descending order:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Array Sort</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Reverse Sort</h4>
<p id="demo"></p>
<script>
const fruits = ["Banana", "Orange", "Apple", "Mango"];
// Sort first, then reverse
fruits.sort();
fruits.reverse();
document.getElementById("demo").innerHTML =
"After sort() and reverse(): " + fruits;
</script>
</body>
</html>

Result View Example
The sort() method sorts numbers as strings, so 25 comes before 100. Use a compare function for numeric sort:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Array Sort</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Numeric Sort</h4>
<p id="demo"></p>
<script>
const points = [40, 100, 1, 5, 25, 10];
// Without compare function: sorts as strings (WRONG for numbers)
const wrong = points.slice();
wrong.sort();
// With compare function: sorts as numbers
const right = points.slice();
right.sort(function(a, b) { return a - b; });
document.getElementById("demo").innerHTML =
"Original: " + points + "<br><br>" +
"Without compare function: " + wrong + " (wrong for numbers)<br>" +
"With compare function: " + right + " (correct)";
</script>
</body>
</html>

Result View Example
The compare function takes two arguments a and b. If the result is:
a is sorted before bb is sorted before a<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Array Sort</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>The Compare Function</h4>
<p id="demo"></p>
<script>
const points = [40, 100, 1, 5, 25, 10];
// Ascending: a - b
const ascending = points.slice();
ascending.sort(function(a, b) { return a - b; });
// Descending: b - a
const descending = points.slice();
descending.sort(function(a, b) { return b - a; });
document.getElementById("demo").innerHTML =
"Original: " + points + "<br><br>" +
"Ascending (a - b): " + ascending + "<br>" +
"Descending (b - a): " + descending;
</script>
</body>
</html>

Result View Example
You can find the lowest or highest value in an array by sorting and accessing the first/last element:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Array Sort</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Find Min or Max with sort()</h4>
<p id="demo"></p>
<script>
const points = [40, 100, 1, 5, 25, 10];
// Sort ascending to find min and max
const sorted = points.slice();
sorted.sort(function(a, b) { return a - b; });
const min = sorted[0];
const max = sorted[sorted.length - 1];
document.getElementById("demo").innerHTML =
"Array: " + points + "<br><br>" +
"Sorted ascending: " + sorted + "<br>" +
"Minimum value: " + min + "<br>" +
"Maximum value: " + max;
</script>
</body>
</html>

Result View Example
Document in project
You can Download PDF file.