#Sorting numerical values in an array.
11 messages · Page 1 of 1 (latest)
Needs a callback?
Apparently, that's what this guide im following is claiming
Callback function as in
const number = [1,4,6,2,3];
const numberSorted = number.sort(function (a, b){
return a-b;
});
Why is a-b needed here, what does it represent?
The sort function decides which number is smaller by checking if the value returned is negative, positive, or 0.
If a is larger than b then the return will be positive. If a is smaller than be the return will be negative. If they're equal the return will be 0.
I see, so a and b represent 2 elements of the array?
Yep
Aight, thanks