Common version numbers such as
2.0.1 compared to 2.0.2, 2.0.2 is newer than 2.0.1.
So how to compare this version number
Here is a relatively simple implementation of the scheme
2.0.1 This format can be split into multiple parts
If the 2 here is the big version, 0 is the child version, and 1 is the fixed version number.
Then we can split the 2.0.1 into N parts
After splitting the n section, you can do the comparison by traversing
Directly on the code
Oc:
determine if aversion is greater than bversion example: a.nsstring*aversion =@"2.4.2"; NSString*bversion =@"2.3.5"; [Self compareversion:aversion biggerthan:bversion] is trueb.nsstring*aversion =@"2.3.5"; NSString*bversion =@"2.3.5"; [Self compareversion:aversion biggerthan:bversion] is falsec.nsstring*aversion =@"0.13.5"; NSString*bversion =@"2.3.5"; [Self compareversion:aversion biggerthan:bversion] is falsed. Determine that the a version number is not less than the B version number NSString*aversion =@"2.0.0"; NSString*bversion =@"2.3.5";//is the current version number(![self compareversion:aversion biggerthan:bversion]) is true+ (BOOL) compareversion: (NSString *) aversion Biggerthan: (NSString *) bversion{Nsarray<nsstring *> *aarray = [aversion componentsseparatedbystring:@"."]; Nsarray<nsstring *> *barray = [bversion componentsseparatedbystring:@"."]; Nsuinteger Loopcount=MIN (Aarray.count,barray.count); for(inti =0; i < Loopcount; i++) { if(Aarray[i].integervalue >barray[i].integervalue) { returnYES; }Else if(Aarray[i].integervalue <barray[i].integervalue) { returnNO; } } returnAarray.count >Barray.count;}
Java:
determine if aversion is greater than bversion example: a.string aversion= @ "2.4.2"; String bversion= @ "2.3.5"; compareappversion (aversion,bversion);//is Trueb.string aversion= @ "2.3.5"; String bversion= @ "2.3.5"; compareappversion (aversion,bversion);//falsec.string aversion= @ "0.13.5"; String bversion= @ "2.3.5"; compareappversion (aversion,bversion);//falseD. Determine that the a version number is not less than the B version number string aversion= @ "2.0.0"; String bversion= @ "2.3.5";//is the current version number(!compareappversion (aversion,bversion)) is trueprotected Booleancompareappversion (String aversion,string bversion) {string a= (Aversion! =NULL? Aversion: ""); String b= (bversion! =NULL? Bversion: ""); List<String> Astringarray = arrays.aslist (a.split ("\ \"))); List<String> Bstringarray = arrays.aslist (b.split ("\ \"))); intLoopcount =math.min (Astringarray.size (), bstringarray.size ()); for(inti = 0; i < Loopcount; i++){ Try{ if(Integer.parseint (Astringarray.get (i)) >Integer.parseint (Bstringarray.get (i))) { return true; } Else if(Integer.parseint (Astringarray.get (i)) <Integer.parseint (Bstringarray.get (i))) { return false; } } Catch(Exception e) {//Logger.warn ("compareappversion version comparison Error" +e.getmessage ()); //Todo:handle Exception } } returnAstringarray.size () >bstringarray.size ();}
Javascript
-----JavaScript------Please refer to Java for the method of judgingfunctioncompareversion (aversion,bversion) {varA = (aversion! =NULL? Aversion: ""); varb = (bversion! =NULL? Bversion: ""); varAstringarray = A.split (".")); varBstringarray = B.split (".")); varLoopcount =math.min (Astringarray.length, bstringarray.length); for(vari = 0; i < Loopcount; i++){ if(parseint (Astringarray[i]) >parseint (Bstringarray[i])) { return true; } Else if(parseint (Astringarray[i]) <parseint (Bstringarray[i])) { return false; } } returnAstringarray.length >bstringarray.length;}
Version number comparison scheme and reference code (OBJECTIVE-C,JAVA,JAVASCRIPT)