Home >Web Front-end >JS Tutorial >Analysis of array sort and reverse usage in Javascript_javascript skills
The examples in this article describe the usage of array sort and reverse in Javascript. Share it with everyone for your reference. The specific analysis is as follows:
Thesort() method is used to sort the elements of an array.
reverse() reverses the order of the elements in the array
First let’s try the following code:
What will the output be:
[ 10, 15, 5, 0, 1 ]
reverse() is simply to reverse the array, so the next thing I want to complain about is sort()
What’s going on?
Actually, will use toString() transformation inside the sort() function, and String comparison is through ASCII, so if we need to sort, it is better to write a sort() ourselves.
Current output:
[ 0, 1, 5, 10, 15 ]
A simpler way to write it is to use return value2 - value1;
inside compare()I hope this article will be helpful to everyone’s JavaScript programming design.