<> 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); }

Technology
©2019-2020 Toolsou All rights reserved,
C Review of basic language knowledge Go Language learning notes (GUI programming )Java Misunderstanding —— Method overloading is a manifestation of polymorphism ? How to achieve low cost and high stability for cloud native applications ?elementui Shuttle box el-transfer Display list content text too long C/C++ Memory model Element-Ui assembly Message Message prompt , alert Popup C# Making a simplified version of calculator Python In pycharm editor Interface style modification Tiktok refresh progress bar ( Two little balls turn ), The code is simple