Version Comparision method

Hello all,

I have a small problem im finding hard to solve, I need a method to compare version numbers.

The version numbers will always be in the same format, for example;

0.0.3

or

1.0.3

etc

I need to construct a method to compare these

1
2
3
4
5

bool IsHigher(String number1, String number2)
{

}


could anyone please help me with this?

Thanks in advance
You basically want to compare the two versions 0.0.3 and 1.0.3 to see if they match?

1
2
3
4
5
6
string version1 = "0.0.3";
string version2 = "1.0.3";
if (version1 < version2)
{
//do your magic
}

I have a feeling this is not really what you're after, could you elaborate your question?
sorry,

I just mean how could I convert these two "Strings" into integer values, so that they can be compared to see if a string1 is higher than string2?

I need to extract the digits eg: 003 and 103 -(removing the "." from each)

then a comaprison against both numeric values retrieved.

I dont know how else to elaborate that
characters, just like integers - have values.

You compare the strings contents to see which has a higher value - in this case 1.0.3 would be.

 
if (version003 < version103)

Is equal to TRUE.

Just tried it... It works.
Last edited on
that doesnt work for all cases...

this does

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
bool Update::IsVersionHigher(wxString vnumber1, wxString vnumber2)
{
        long* version=new long[3];

        version[0] = vnumber1[0]-48;
        version[1] = vnumber1[2]-48;
        version[2] = vnumber1[4]-48;

        long* version2 = new long[3];

        version2[0] = vnumber2[0]-48;
        version2[1] = vnumber2[2]-48;
        version2[2] = vnumber2[4]-48;


        if ( ( (version[0]*10000) + (version[1]*100) + version[2] ) < ( (version2[0]*10000) + (version2[1]*100) + version2[2] ) )
        {
           return true;
        }
        else{return false;}
}
Last edited on
that doesnt work for all cases...


Correct. Consider a counter example (11.0.3 vs. 2.0.3).

this does


Well, it's better, I guess. How about a solution that extracts the numeric parts of the string, converts them to integers using stringstreams, and then compares them in the correct order?
Topic archived. No new replies allowed.