private static int compare ( String version1 , String version2 ) { if ( version1 == null && version2 == null ) { return 0 ; } else if ( version1 == null && version2 != null ) { return - 1 ; } else if ( version1 != null && version2 == null ) { return 1 ; } String [ ] components1 = version1 . split ( "\\." ) ; String [ ] components2 = version2 . split ( "\\." ) ; for ( int i = 0 ; i < components1 . length && i < components2 . length ; i ++ ) { int comparison ; try { comparison = new Integer ( components1 [ i ] ) . compareTo ( new Integer ( components2 [ i ] ) ) ; } catch ( NumberFormatException e ) { comparison = components1 [ i ] . compareTo ( components2 [ i ] ) ; } if ( comparison != 0 ) { return comparison ; } } return components1 . length - components2 . length ; }
Performs pairwise comparison of version strings including nulls and non - integer components .