Today in the process of review, found that the reordering method of the array sort () has been a misunderstanding, re-smooth, in this record ...
There are two methods in the array that can be used to reorder directly: reverse () and sort (), and the reverse () method is the order in which the array items are reversed. By default, the sort () method arranges array items in ascending order, and in this process the sort method invokes the ToString () transformation method of each array item, then compares the resulting string and then determines the sort. Have not noticed this before, always thought that the call sort () method will be directly to an array after the correct ordering, in fact, it is not the case, but the resulting string comparison, their understanding of it has been wrong, and today finally clear ...
The sort () method can accept a comparison function as a parameter so that we have a value that is in front of which value. The following is a simple comparison function.
function Compare (A, b) {
if (a<b) {
return-1;
}else if (a>b) {
return 1;
}else {
return 0;
}
}
Pass it to the sort method.
Array reordering method