Array sorting, compared to three-bit values according to the original number of digits, more than three bits to take the back three bit to compare

Source: Internet
Author: User
First step: First to determine the length of the value, first turn him into a string, in judging the number of digits, intercept the back of the three-bit to compare
var arr=[12432,34,535,89002,452,50]
for (Var i=0;i<arr.length;i++) {
Arr[i]=arr[i].tostring ()
if (arr[i].length>3) {
Arr[i]=arr[i].substr (arr[i].length-3)
}
}
The second step: convert the string into a numerical value to compare and judge
b = Arr.map (function (e) {return parseint (e);})
function Compare (value1,value2) {
if (value1 < value2) {
return-1;
}else if (value1 > value2) {
return 1;
}else{
return 0;
}
}
B.sort (Compare);

        以上是根据自己的想法来写的,不知道是否有简洁的办法来实现

Array sorting, compared to three-bit values according to the original number of digits, more than three bits to take the back three bit to compare

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.