add Version Compare Func

This commit is contained in:
6543 2019-10-20 22:02:03 +02:00
parent 1630d47f9c
commit 512faab9cc
Signed by: 6543
GPG Key ID: A1CA74D27FD13271
1 changed files with 32 additions and 2 deletions

View File

@ -117,7 +117,37 @@ public enum Version {
return 3;
}

/**
* @description compare doted formatted Versions
* @return 0|1|2
* 0 = less
* 1 = same
* 2 = more
*/
public static int compare(String A, String B) {
if (A.contains(".") || B.contains(".")) {
// example 2 vs 1.3
if (!(A.contains(".") && B.contains("."))) {
if (A.contains(".")) {
return compare(A,B + ".0");
}
if (B.contains(".")) {
return compare(A + ".0",B);
}
}

return 0;
//normal compare
int a = Integer.parseInt(A.substring(0,A.indexOf(".")));
int b = Integer.parseInt(B.substring(0,B.indexOf(".")));
if (a < b) return 0;
if (a == b) return compare(A.substring(A.indexOf(".")+1),B.substring(B.indexOf(".")+1));
return 2; //if (a > b)
} else {
int a = Integer.parseInt(A);
int b = Integer.parseInt(B);
if (a < b) return 0;
if (a == b) return 1;
return 2; //if (a > b)
}
}
}
}