protected static int compareVersions ( String left , String right ) { int result = 0 ; //while the strings are well formed - the backslashes will be in the exact //same location in equal strings - for version numbers the cost of conversion //should not be incurred //final List<String> subLeft = splitVersion(Convert.fromWellFormed(left)); //final List<String> subRight = splitVersion(Convert.fromWellFormed(right)); final List < String > subLeft = splitVersion ( left ) ; final List < String > subRight = splitVersion ( right ) ; final int subMax = ( subLeft . size ( ) <= subRight . size ( ) ) ? subLeft . size ( ) : subRight . size ( ) ; for ( int x = 0 ; result == 0 && x < subMax ; x ++ ) { if ( isPositiveInteger ( subLeft . get ( x ) ) && isPositiveInteger ( subRight . get ( x ) ) ) { try { result = Long . valueOf ( subLeft . get ( x ) ) . compareTo ( Long . valueOf ( subRight . get ( x ) ) ) ; } catch ( NumberFormatException ex ) { //infeasible path - unless one of the values is larger then a long? if ( ! subLeft . get ( x ) . equalsIgnoreCase ( subRight . get ( x ) ) ) { result = subLeft . get ( x ) . compareTo ( subRight . get ( x ) ) ; } } } else { result = subLeft . get ( x ) . compareTo ( subRight . get ( x ) ) ; } if ( result != 0 ) { return result ; } } if ( subLeft . size ( ) > subRight . size ( ) ) { result = 1 ; } if ( subRight . size ( ) > subLeft . size ( ) ) { result = - 1 ; } return result ; }
Compare version numbers to obtain the correct ordering .