Re: how to compare two version strings in java
anywherenotes@gmail.com wrote:
Hi,
How would I compare two version strings, for example:
10.1.2.0 is greater than 10.0.0.0
and
10.1.2.0 is also greater than 9.0.0.0
however if I would compare those as String objects, obviously 9.0.0.0
would be greater than 10....
Is there a good way of doing it?
It's possible to split the string into numbers and do numeric
conversion, but if there's already a standard java class/method
handling this I'd like to use it.
Thank you.
Yeah, I think you have to do the comparison yourself. I don't think
there's a standard class to do it for you.
package versionstring;
public class VersionString implements Comparable<VersionString> {
final private String version;
public VersionString(String version) {
if( version.length() == 0 )
throw new IllegalArgumentException(
"\"version\" may not be zero length" );
String temp[] = version.split("\\.");
for( String s : temp )
{
if( s == null )
throw new IllegalArgumentException(
"\"version\" contains a null version sub-string");
if( s.length() == 0 )
throw new IllegalArgumentException(
"\"version\" contains a zero lenght version sub-string" );
Integer.parseInt(s);
}
this.version = version;
}
@Override
public String toString()
{
return this.version;
}
@Override
public int compareTo(VersionString other)
{
if( other == this )
return 0;
if( other.version.equals( this.version ) )
return 0;
String otherVersionList[] = other.version.split("\\.");
String thisVersionList[] = this.version.split("\\.");
int thisIndex = 0;
for( String otherVersion : otherVersionList )
{
if( thisIndex >= thisVersionList.length )
break;
int thisVersionNum =
Integer.parseInt( thisVersionList[thisIndex] );
int otherVersionNum = Integer.parseInt(otherVersion);
if( thisVersionNum != otherVersionNum )
return (thisVersionNum - otherVersionNum < 0) ? -1 : 1;
thisIndex++;
}
if( thisVersionList.length != otherVersionList.length )
return (thisVersionList.length -
otherVersionList.length < 0) ? -1 : 1;
else
return 0;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
VersionString s10 = new VersionString("10");
VersionString s10b = new VersionString("10");
StringBuilder sb = new StringBuilder();
sb.append("1");
sb.append("0");
VersionString s10c = new VersionString( sb.toString() );
VersionString s10_9 = new VersionString("10.9");
VersionString s10_8 = new VersionString("10.8");
VersionString s9 = new VersionString("9");
VersionString s9_7 = new VersionString("9.7");
compare( s10, s9 );
compare( s9, s10 );
compare( s10, s10 );
compare( s10, s10b );
compare( s10, s10c );
compare( s9, s9 );
compare( s10, s10_9 );
compare( s10_9, s10 );
compare( s10_8, s10_9 );
compare( s10_9, s10_8 );
compare( s9_7, s10_8 );
compare( s10, s9_7 );
compare( s9_7, s10 );
try {
compare( s9_7, null );
} catch( Exception ex )
{ System.out.println( ex ); }
construct( null );
construct( "" );
construct( "abc" );
construct( "10.9a" );
construct( "7.0" );
construct( "6." );
construct( ".1" );
}
private static void compare( VersionString s1, VersionString s2 ) {
System.out.print("Compare " + s1 + " and " + s2 + " : ");
System.out.println( s1.compareTo(s2) );
}
private static void construct( String s ) {
System.out.print("Constructing \"" +s+ "\" - " );
try {
new VersionString( s );
} catch( Exception ex )
{ System.out.print( ex ); }
finally { System.out.println(""); }
}
}