mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-24 18:04:54 +08:00
修复VersionComparator对1.0.3及1.0.2a比较有误的问题
This commit is contained in:
parent
04a257aec8
commit
b146c90309
@ -12,6 +12,9 @@
|
||||
|
||||
package org.dromara.hutool.core.comparator;
|
||||
|
||||
import org.dromara.hutool.core.convert.Convert;
|
||||
import org.dromara.hutool.core.regex.PatternPool;
|
||||
import org.dromara.hutool.core.regex.ReUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.core.text.split.SplitUtil;
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
@ -87,6 +90,15 @@ public class VersionComparator implements Comparator<String>, Serializable {
|
||||
diff = v1.length() - v2.length();
|
||||
if (0 == diff) {
|
||||
diff = v1.compareTo(v2);
|
||||
}else {
|
||||
// https://gitee.com/dromara/hutool/pulls/1043
|
||||
//不同长度的先比较前面的数字;前面数字不相等时,按数字大小比较;数字相等的时候,继续按长度比较,
|
||||
final int v1Num = Convert.toInt(ReUtil.get(PatternPool.NUMBERS, v1, 0), 0);
|
||||
final int v2Num = Convert.toInt(ReUtil.get(PatternPool.NUMBERS, v2, 0), 0);
|
||||
final int diff1 = v1Num - v2Num;
|
||||
if (diff1 != 0) {
|
||||
diff = diff1;
|
||||
}
|
||||
}
|
||||
if(diff != 0) {
|
||||
//已有结果,结束
|
||||
|
@ -53,4 +53,10 @@ public class VersionComparatorTest {
|
||||
final VersionComparator other = new VersionComparator();
|
||||
Assertions.assertNotEquals(first, other);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void versionComparatorTest7() {
|
||||
final int compare = VersionComparator.INSTANCE.compare("1.12.2", "1.12.1c");
|
||||
Assertions.assertTrue(compare > 0);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user