<> use sort(function(a,b){return a-b}) The principle of sorting arrays
<> case : function sort(){ var _arr1=[8,95,31,1,5];
console.log(_arr1.sort());// output 1,31,5,8,95, Because the element is converted to a string first , Compare with the first character of a string ,
// If the first character is the same , Continue comparing the second character // In view of the above situation , Use the following method to solve the problem
console.log(_arr1.sort(function(a,b){//a,b Just parameters , Other characters can be used return a-b;
}));// output 1,5,8,31,95 } sort();
sort(function(a,b){return a-b}) The principle of sorting arrays :
_arr1.sort(function(a,b){ return a-b; });
Using this method in the above code is equivalent to giving the following function _arr1.sort An argument was passed ,fx To receive this argument
_arr1.sort=function(fx){//fx Used to receive calls _arr1.sort The parameter passed from , here fx=function(a,b){return
a-b;} // Then compare the bubble sort from front to back , Sort in descending order var _empty=null; for(var n=0;
n<_arr1.length-1;n++){ for(var i=0;i<_arr1.length-1; i++){
// because fx After receiving the parameters fx=function(a,b){return a-b}, So call here fx The last two parameters are passed
// It's equivalent to giving function Two parameters were passed ,a=_arr1[i+1],b=_arr[i]( Because it's returned a-b), Subtract the last digit from the previous digit
if(fx(_arr1[i+1],_arr1[i])<0){// Judge by i+1 Elements minus No i Are the elements less than 0, If less than 0, explain i+1 The number in the position is small , Exchange
// for example 95-8 greater than 0, No exchange ,31-95 less than 0, Exchange , Put the smaller value first _empty=_arr1[i]; _arr1[i]=_arr1[i+1];
_arr1[i+1]=_empty; } } } console.log(_arr1); }

©2019-2020 Toolsou All rights reserved,
Forbes China Auto rich list : He xiaopengdi 11 Li Xiangdi 14 Li Bindi 15 Change one's mind ! Tesla starts to deliver made in China to European market Model 3 The difference between memory overflow and memory leak , Causes and Solutions Character recognition technology of vehicle license plate based on Neural Network Vue Transfer parameters and receiving of page Jump SparkSQL Achieve partition overlay write 1190 Reverses the substring between each pair of parentheses leetcode Note 14 : The second biggest obstacle to motivating others in R & D management Chrome OS, For programmers and Windows What does it mean ? Internet Marketing JAVA Convert a string to a numeric type