[JS] array sorting functions sort pit entry

var arr = [100,99];
arr1 = arr.sort();

Q: At this point arr and how much value each arr1

arr = arr1 = [100,99]

Inference: Array.sort () is an array in ascending order by default, but the elements in the array will be converted into an array forced to do the comparison, and because "100" < "99", then the final result is ascending [100,99]

 

Extension: If an array element of the object, the object will force the comparison process is converted into a string (note: not turn into JSON), into a unified "[object Object]", so if all the elements within the array are objects, ordering the results consistent with the original array

 

Published 35 original articles · won praise 18 · views 370 000 +

Guess you like

Origin blog.csdn.net/TXX_c/article/details/82182350