6 Replies Latest reply on Mar 21, 2011 1:55 PM by rtalton

# 9 is more than 10 ??

I want to compare one version number to another, however I'm getting unexpected results as follows:

trace(('1.0.1' > '1.0.2') ? 'Yes' : 'No'); // result is "No"

trace(('1.0.9' > '1.0.10') ? 'Yes' : 'No'); // result is "Yes"

With the second result, changing 10 to 11 to read "1.0.11" makes no difference; it still thinks 9 is more.

Any reason why this is happening? Thanks in advance.

• ###### 1. Re: 9 is more than 10 ??

You appear to be comparing Strings.

• ###### 2. Re: 9 is more than 10 ??

Ok, what would be the correct way to do it?

• ###### 3. Re: 9 is more than 10 ??

You need to break the string apart. From your example it looks like you want

to compare software versions. First break apart the string parts you want to

compare.

Here's some quick example code that may or may not work (I need more

coffee). I just threw it together but it should demonstrate the basic idea.

private function versionCompare(ver1:String, ver2:String):Boolean{

var majorVersion1:int =

parseInt(ver1.substr(0,ver1.indexOf(".")));

var majorVersion2:int =

parseInt(ver2.substr(0,ver2.indexOf(".")));

if(majorVersion1!=majorVersion2){

return majorVersion1<majorVersion2;

} else {

//do the same for minor version and so on

...

}

}

• ###### 4. Re: 9 is more than 10 ??

I see what you mean, thanks, much appreciated.

• ###### 5. Re: 9 is more than 10 ??

Happy to help!

• ###### 6. Re: 9 is more than 10 ??

Be very careful here! If you have complete control over what values to expect in the version numbers then you might be ok. If not, you will find exceptions caused by all kinds of variations in version numbers: leading zeroes, letters mixed with numbers, etc.

Example: '1.0.01' compared to '1.0.1'

Converting each part to a number will yield "1" and "1" for the last sections--so they would be equivalent. Same thing could happen if characters such as "BETA", "K", "final" etc. are used and mixed with the numeric characters.

You'd have to be absolutely certain to check for many variations, unless you knew precisely what format the versioning will be ahead of time. You may know this already; I'm just posting this for the benefit of anyone else reading this post.