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