ZaZaKi, a web developer Between Manchester UK & Rotterdam NL. © 2015-2024.

JS Numeric Sort

Descending order and Ascending order

 
<!DOCTYPE html>
<html>
<body>

<p>Descending order <br><span id="demo1"></span></p>
<p>Ascending order <br><span id="demo2"></span></p>

<script>

// array
var points = [40, 90, 100, 80, 1, 10, 20, 30, 50, 60, 70,];

//sort the array in descending order
points.sort(function(a, b){return b - a}); // see b - a !
document.getElementById("demo1").innerHTML = points;

// sort the array in ascending order
points.sort(function(a, b){return a - b}); // see a - b !
document.getElementById("demo2").innerHTML = points;

</script>

</body>
</html
Categroy:
Front-end , javascripts