Maybe you have been using the sort of array in javascript.
Maybe you have always believed that it will give you the right result.
At least I used to think so, until one day, I saw the following code:
The code copy is as follows:
[5,10,1].sort();
Perhaps the result is a bit unexpected. The results are as follows:
The code copy is as follows:
[1,10,5]
After careful investigation, I found that the default sort method was not sorted according to the plastic shaping data, but used string matching method.
In other words, this 1 in 10 causes the error in the above code.
Of course, there are many solutions, you can pass in the callback function into the sort method.
The code copy is as follows:
[5,10,1].sort(function(x,y){
if(x>y) {return 1;
}else{
return -1
}
}
);
This will get the results you expect.
If you find it by chance, record it to prevent forgetting.