7

How can I compare two Version number strings?

For example: 3.1.1 and 3.1.2.5.4

Now I need to find out if 3.1.2.5.4 is higher than 3.1.1 but I don't know how to do this. Can anybody help me?

Thanks in advance!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
AmiiQo
  • 353
  • 1
  • 7
  • 17
  • http://stackoverflow.com/questions/7004655/comparing-strings-with-the-format-2-0-1-2-0-09 – Satheesh Sep 03 '13 at 14:04
  • possible duplicate of [compare version numbers in objective c](http://stackoverflow.com/questions/1978456/compare-version-numbers-in-objective-c) – Amar Oct 04 '13 at 11:05

3 Answers3

11

Sample Code :

NSString* v1 = @"3.1.1";
NSString* v2 = @"3.1.2.5.4";

if ([v1 compare:v2 options:NSNumericSearch] == NSOrderedDescending) {
    NSLog(@"%@ is greater than %@",v1,v2);
}

From the Apple Documentation for Comparing and sorting strings.

Bhavin
  • 27,155
  • 11
  • 55
  • 94
  • 3
    Edge case: does not work if one ends in `.0` and the other does not, e.g. `@"3.1"` and `@"3.1.0"`. The split solutions (see @pArAs's answer), or padding, can deal with this. – CRD Sep 03 '13 at 21:33
  • 1
    That's true. But version checking can be tedious. Check out my NSString category that catches all the edge cases on github; https://github.com/stijnster/NSString-compareToVersion `[@"1.2.2.4" compareToVersion:@"1.2.2.5"];` – Stijnster Jul 14 '16 at 06:52
2

Yes, you can compare the versions, please refer the code below:

public class Comparision {

    string ver1, ver2;
    public static void main(String args[]){
    string ver1Split[] = ver1.split('.');
    string ver2Split[] = ver2.split('.');

    for (int i = 0; i < ver1Split.length; ++i) {
        if (ver2Split == i) {
            return ver1 + " is larger";
        }

        if (ver1Split[i] == ver2Split[i]) {
            continue;
        }
        else if (ver1Split[i] > ver1Split[i]) {
            return ver1 + " is larger";
        }
        else {
            return ver2 + " is larger";
        }
        if (ver1Split.length != ver2Split.length) {
            return ver2 + " is larger";
        }
        return "versions are equal";
    }
}
Stunner
  • 12,025
  • 12
  • 86
  • 145
Paras
  • 3,197
  • 2
  • 20
  • 30
  • you can initialize the String variables ver1 and ver2 by providing the actual version numbers. – Paras Sep 03 '13 at 14:17
  • yours works too, but Vin's solution is easier for my specific needs. But nevertheless thanks! – AmiiQo Sep 03 '13 at 14:20
0

Objective-C:

- (BOOL)isVersion:(NSString *)arg1 higherThan:(NSString *)arg2 {

    NSMutableString * v1 = arg1.mutableCopy;
    NSMutableString * v2 = arg2.mutableCopy;

    NSMutableArray * parts1 = [v1 componentsSeparatedByString:@"."].mutableCopy;
    NSMutableArray * parts2 = [v2 componentsSeparatedByString:@"."].mutableCopy;

    if (parts1.count > parts2.count) {

        NSInteger diff = parts1.count - parts2.count;

        for (NSInteger i = diff; i<parts1.count; i++) {
            [v2 appendString:@".0"];
        }

    } else if (parts1.count < parts2.count){

        NSInteger diff = parts2.count - parts1.count;

        for (NSInteger i = diff; i<parts2.count; i++) {
            [v1 appendString:@".0"];
        }

    }

    parts1 = [v1 componentsSeparatedByString:@"."].mutableCopy;
    parts2 = [v2 componentsSeparatedByString:@"."].mutableCopy;

    NSInteger j = 0;
    for (NSString * num1 in parts1) {
        NSString * num2 = parts2[j];
        if(num1.integerValue > num2.integerValue){
            //break;
            return YES;
        } else if (num1.integerValue < num2.integerValue) {
            //break;
            return NO;
        } else {
            // ==
        }
        j++;
    }

    return NO;
}

Unit test:

- (void)test_isHigherFunc {

    XCTAssert([self isVersion:@"4.1.2.1" higherThan:@"4.1.2.0"]);

    XCTAssertFalse([self isVersion:@"4.1.2.0" higherThan:@"4.1.2.0"]);

    XCTAssert([self isVersion:@"4.1.2.0" higherThan:@"4.1.1.0"]);

    XCTAssertFalse([self isVersion:@"3.1.2.0" higherThan:@"4.1.1.0"]);

    XCTAssertFalse([self isVersion:@"4.2.2.0" higherThan:@"4.3.1.0"]);

    XCTAssert([self isVersion:@"5.2" higherThan:@"4.3.1.0"]);

    XCTAssertFalse([self isVersion:@"6.2" higherThan:@"7.3.1.0"]);

    XCTAssert([self isVersion:@"6.2" higherThan:@"5"]);

    XCTAssert([self isVersion:@"6.2.0" higherThan:@"5.9"]);

    XCTAssert([self isVersion:@"1.1.1.1.1.1.1.1" higherThan:@"1.1.1.1.1.1.1.0"]);

    XCTAssert([self isVersion:@"2.0" higherThan:@"1"]);
}
Aleksandr B.
  • 505
  • 4
  • 6