JS comparison version number (ii)

Source: Internet
Author: User

In the previous article--js comparison version number (a), the author uses JS to use its own method complex processing the next only a pair of string version number comparison. Why is it complicated, because it does not apply to a bunch of version number comparisons, need more code, then how to solve it?

In fact,JS has been packaged for us sorting function sort (), can be a good convenience to solve the string comparison, sorting problem, really "pale no place, get all the time."

There are good 2 examples [1], moving yards here:

Test 1:

var a=["Ten", "5", "Max", "+", "" "," 1 ";            Console.log (a);      Console.log (A.sort ());   

Results:

["Ten", "5", "Max", "+", "+", "1"] ["1", "10", "1000", "25", "40", "5"]

Test Two:

function Sortnumber (b) {return A- B} var a=["Ten", "5", "Max", "+", "" "," 1 ";            Console.log (a);      Console.log (A.sort (Sortnumber));   

Results:

["Ten", "5", "Max", "+", "+", "1"] ["1", "5", "10", "25", "40", "1000"]

Seems to have achieved the goal, but is it really so? Add "1." to each element in the test array:

Test Three:

function Sortnumber (b) {return A- B} var a=["1.10", "1.5", "1.40", "1.25", "1.1000", "1.1";            Console.log (a);      Console.log (A.sort (Sortnumber));   

Results:

["1.10", "1.5", "1.40", "1.25", "1.1000", "1.1"] [

Not quite the same as expected, 1.10 ran to 1.5 front, what ghost? Is there no way, sort () to solve the problem of version number sorting?

The answer is in the negative. One problem with the sort (sortnumber) solution is that the integer numeric string group can be sorted well, because Sortnumber can compare sizes to integers. Imagine if there is a function (sortversions) that can compare the size of the string in the format of the version number, then can sort (sortversions) order the string groups in any version number format? If you can, the comparison of the previous one will be useful. Just make an improvement on the function in the previous article, OK. As a result, the version number of the upgrade version is compared--the sort is formed:

JS version number comparison-- version 2.0

//assume that the number of strings per section is below 5 bits//remove array null value | | Spaceif(!Array.prototype.trim) {Array.prototype.trim=function(){     varArr=[]; This. ForEach (function(e) {if(E.match (/\s+/)) Arr.push (e); })  returnarr;} }//extracting a number partfunctionTonum (a) {varA=a.tostring ();varC=a.split (/\d/). Trim (); varNum_place=["", "0", "xx", "0000", "],r="Num_place.reverse ();  for(vari=0;i<c.length;i++){    varlen=c[i].length; C[i]=r[len]+C[i]; } varres= C.join ("); returnRes;} //extracting charactersfunctionToChar (a) {varA=a.tostring (); varC=a.split (/\.| \d/). Join ('); returnC;}functionSortversions (A, b) {var_a1=tonum (a), _b1=Tonum (b); if(_A1!==_B1)return_a1-_b1;Else{_a2= ToChar (a). charCodeAt (0). toString (16); _B2= ToChar (b). charCodeAt (0). toString (16); return_a2-_b2; }}vararr1=["10", "5", "40", "25", "1000", "1"];vararr2=["1.10", "1.5", "1.40", "1.25", "1.1000", "1.1"];console.log (Arr1.sort (sortversions)) Console.log (Arr2.sort (sortversions) )

Results:

["1", "5", "Ten", "+", "+", "+"] ["1.1", "1.5", "1.10", "1.25", "1.40", "1.1000"]

One more set:

Test four:

//tested in version 2.0 continued testing on the Chrome console var arr3=["2.0a", "2.1", "2.1a", "2.10b", "2.10a", "2.1b"];console.log (ARR3) Console.log (Arr3.sort ( sortversions))

Results:

["2.0a", "2.1", "2.1a", "2.10b", "2.10a", "2.1b"["2.0a", "2.1", "2.1a", "2.1b", "2.10a", "2.10b"]

At this point, the comparison of the version number is over.

Summary:

1. What knowledge is used in JS: regular expression, Console,array () and its extended method, sort () and its parameters, word representable ASCII code

2. The text can be seen, divergent thinking is not enough, need more exercise: often some seemingly rare problems, flexible, jump out of the "box", will find that the difficulty originally so basic

3. Pay tribute to other posts, the code is locally tested correctly, it's not easy to post it right

Unresolved issues in the article:

1. Comparison of version numbers with multiple characters

2. Can you write a sort of program yourself?

3. Other bugs and deficiencies

Citations: [1]javascript sort () method http://www.w3school.com.cn/jsref/jsref_sort.asp

JS comparison version number (ii)

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.