public static int compareVersions ( String v1 , String v2 ) { String [ ] components1 = split ( v1 ) ; String [ ] components2 = split ( v2 ) ; int diff ; int length = Math . min ( components1 . length , components2 . length ) ; for ( int i = 0 ; i < length ; i ++ ) { String s1 = components1 [ i ] ; String s2 = components2 [ i ] ; Integer i1 = tryParseInteger ( s1 ) ; Integer i2 = tryParseInteger ( s2 ) ; if ( i1 != null && i2 != null ) { diff = i1 . compareTo ( i2 ) ; } else { // lets assume strings instead diff = s1 . compareTo ( s2 ) ; } if ( diff != 0 ) { return diff ; } } diff = Integer . compare ( components1 . length , components2 . length ) ; if ( diff == 0 ) { if ( v1 == v2 ) { return 0 ; } /* if v1 == null then v2 can't be null here (see 'if' above). So for v1 == null its always smaller than v2 */ ; return v1 != null ? v1 . compareTo ( v2 ) : - 1 ; } return diff ; }
Compares two version strings such that 1 . 10 . 1 > 1 . 4 etc