The code looks like this:
var arr = [5,32,28,66,2,15,3]; arr.sort(function(a1,a2){ return a1-a2; //a2-a1 Input reverse order}); console.log(arr); console.log(arr); console.log(arr.reverse()); //reverse reverse the order of elements in the array var arr2 = ['hezihao','chensan','xiaomin','lishi'] arr2.sort(); console.log(arr2); var arr3 = ['6','8','0','7'] arr3.sort(); console.log(arr3);Enter the result in the above code:
Arrays are sorted alphabetically without passing in parameters
To sort numbers by the size of the values, you must use a sorting function.
If a1-a2 is a negative number, it is in ascending order. If a2-a1 is a positive number, it is in descending order. You can also use the reverse() method to reverse the order of elements in the array.