I'm learning Javascript and I faced a problem with sorting numbers I don't understand how work sorting function and I find an other way to sort the numbers but it was with list not with arrays I need explanations. I also see this link : Sorting array with numbers without sort() method
const points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo1").innerHTML = points;
points.sort(function(a, b) {
return a - b
});
document.getElementById("demo2").innerHTML = points;
document.getElementById("demo3").innerHTML = points.sort();
unsorted:
<div id="demo1"></div>
sorted:
<div id="demo2"></div>
sorted alphabetically:
<div id="demo3"></div>
Array.prototye.sort()
and you will understand the role of the callback function: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… . If you want to learn how sort algorithms work in general then maybe you should google "bubble sort" or "quicksort". – Punchinello-
operator. To use the-
operator with.sort()
you dopoints.sort((a,b) => a - b)
– Snowslide