Compares two version strings. Use this instead of String.compareTo() for a non-lexicographical comparison that works for version strings. e.g. "1.10".compareTo("1.6"). @param str1 a string of ordinal numbers separated by decimal points. @param str2 a string of ordinal numbers separated by deci
(String str1, String str2)
| 113 | * @note It does not work if "1.10" is supposed to be equal to "1.10.0". |
| 114 | */ |
| 115 | public static Integer versionCompare(String str1, String str2) { |
| 116 | String[] vals1 = str1.split("\\."); |
| 117 | String[] vals2 = str2.split("\\."); |
| 118 | int i = 0; |
| 119 | // set index to first non-equal ordinal or length of shortest version string |
| 120 | while (i < vals1.length && i < vals2.length && vals1[i].equals(vals2[i])) { |
| 121 | i++; |
| 122 | } |
| 123 | // compare first non-equal ordinal number |
| 124 | if (i < vals1.length && i < vals2.length) { |
| 125 | int diff = Integer.valueOf(vals1[i]).compareTo(Integer.valueOf(vals2[i])); |
| 126 | return Integer.signum(diff); |
| 127 | } |
| 128 | // the strings are equal or one string is a substring of the other |
| 129 | // e.g. "1.2.3" = "1.2.3" or "1.2.3" < "1.2.3.4" |
| 130 | else { |
| 131 | return Integer.signum(vals1.length - vals2.length); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | public static void setUI(String className) { |
| 136 | try { |
no test coverage detected