I have this version comparison function in java.
Any cleaner way to write this without all the nested 'if' statements?
Note that the suggestions must be in Java code only.
@Override
public int compareTo(Version o)
{
int result = this.major.compareTo(o.major);
if (result == 0)
{
result = this.minor.compareTo(o.minor);
if (result == 0)
{
result = this.minor2.compareTo(o.minor2);
if (result == 0)
{
result = this.suffix.compareTo(o.suffix);
}
}
}
return result;
}
Edit:
Just to be clear on the 'type' of the fields of the Version class:
Integer major, minor, minor2;
String suffix;
Version v1 = Version.valueOf("1.2.3"); Version v2 = Version.valueOf("1.2.0"); System.out.println(v1.compareTo(v2));\$\endgroup\$