JS Implementation version number comparison function

Source: Internet
Author: User

JS implementation of a client version number statistics, the function can be implemented in the version number of the digital version of the statistics, such as 2.2.3 <2.2.3.1, 1.2.3<2.0.0, the principle is from the higher version number to the minor version of the comparison

1 if(!Array.prototype.trim) {2array.prototype.trim=function(){3                     varArr=[];4                      This. ForEach (function(e) {5                         if(E.match (/\s+/) ) Arr.push (e);6                     })7                     returnarr;8                 }9             }Ten  One functionVersion_compare (A, b) { A                                 varA_str=a.name.tostring (); -                                 varA_version=a_str.split (/\d+/). Trim (); -                                 varB_str=b.name.tostring (); the                                 varB_version=b_str.split (/\d+/). Trim (); -  -                                 varAlength =a_version.length; -                                 varBlength =b_version.length; +                                 varMinl =math.min (alength,blength); -                                     varR =NULL +                                      for(vari = 0; i < minl; i++) { A                                        //here to turn into number type, otherwise the comparison will appear 15<6 situation, may be due to regular matching, has not been explored in detail why at                                         varAnum =NewNumber (A_version[i]); -                                         varBnum =NewNumber (B_version[i]); -                                         if(Anum <bnum) { -R = 1 -                                              Break; -  in}Else if(Anum >bnum) { -r =-1; to                                              Break; +}Else if(i = = minL-1 && Anum = =bnum) { -  the                                             if(alength>minl) { *R = 1; $                                                  Break;Panax Notoginseng}Else if(blength>minl) { -r =-1; the                                                  Break; +                                             } A                                         } the  +                                     } -                                 returnR; $}

JS Implementation version number comparison function

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.