However, we find the problem in use, where the array ordering method is not sorted according to the size of the figure we imagined, but rather the original data was changed according to the string test results. That's not what we want.
So how do we get what we want to sort by the size of the numbers in our minds? We can write a function to implement it ourselves.
Copy Code code as follows:
var values = [0, 1, 5, 10, 15];
ASC Ascending function
function Compareasc (value1, value2) {
if (value1 > value2) {
return 1;
else if (value1 < value2) {
return-1;
} else {
return 0;
}
}
Desc Descending function
function Comparedesc (value1, value2) {
if (value1 > value2) {
return-1;
else if (value1 < value2) {
return 1;
} else {
return 0;
}
}
Values.sort (COMPAREASC);
Console.log (values); [0, 1, 5, 10, 15]
Values.sort (COMPAREDESC);
Console.log (values); [15, 10, 5, 1, 0]