js sort asendenet 1

js sort asendenet

var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a-b});

Here is what the above code is Doing:
1. We have an array of numbers.
2. We use the sort() method, which takes a callback function that compares two values.
3. The callback function has two parameters which are the two values being compared.
4. The function returns a negative number if the first value is smaller, a positive number if the first value is larger, and 0 if they’re equal.
5. The sort() method sorts the array based on the return values of the callback function.

Similar Posts