diff --git a/api/src/main/java/run/halo/app/extension/index/KeyComparator.java b/api/src/main/java/run/halo/app/extension/index/KeyComparator.java index b94d4c7ef3..286bd21800 100644 --- a/api/src/main/java/run/halo/app/extension/index/KeyComparator.java +++ b/api/src/main/java/run/halo/app/extension/index/KeyComparator.java @@ -60,14 +60,6 @@ private int compareNumbers(String a, String b, int startA, int startB) { int i = startA; int j = startB; - // Skip leading zeros for both numbers - while (i < a.length() && a.charAt(i) == '0') { - i++; - } - while (j < b.length() && b.charAt(j) == '0') { - j++; - } - // Compare lengths of remaining digits int lengthA = countDigits(a, i); int lengthB = countDigits(b, j); @@ -114,13 +106,6 @@ private int compareIntegerPart(String a, String b, int startA, int startB, int p int i = startA; int j = startB; - while (i < pointA && a.charAt(i) == '0') { - i++; - } - while (j < pointB && b.charAt(j) == '0') { - j++; - } - int lengthA = pointA - i; int lengthB = pointB - j; if (lengthA != lengthB) { @@ -150,6 +135,7 @@ private int compareFractionalPart(String a, String b, int i, int j) { j++; } + // If one number has more digits left, and they're not all zeroes, it is larger while (i < a.length() && Character.isDigit(a.charAt(i))) { if (a.charAt(i) != '0') { return 1; diff --git a/api/src/test/java/run/halo/app/extension/index/KeyComparatorTest.java b/api/src/test/java/run/halo/app/extension/index/KeyComparatorTest.java index 959980a0aa..acca24d7da 100644 --- a/api/src/test/java/run/halo/app/extension/index/KeyComparatorTest.java +++ b/api/src/test/java/run/halo/app/extension/index/KeyComparatorTest.java @@ -388,6 +388,13 @@ public void pureNumbersTest() { assertThat(comparator.compare("124", "123")).isGreaterThan(0); // Leading zeros assertThat(comparator.compare("00123", "123") > 0).isTrue(); + assertThat(comparator.compare("0", "0")).isEqualTo(0); + assertThat(comparator.compare("0", "0000")).isLessThan(0); + assertThat(comparator.compare("0x", "0")).isGreaterThan(0); + assertThat(comparator.compare("0", "1")).isLessThan(0); + assertThat(comparator.compare("1", "0")).isGreaterThan(0); + assertThat(comparator.compare("001", "0")).isGreaterThan(0); + assertThat(comparator.compare("0x5e", "0000")).isLessThan(0); } @Test @@ -430,6 +437,7 @@ public void decimalStringsTest() { assertThat(comparator.compare("123.46", "123.45")).isGreaterThan(0); // Decimal equivalence assertThat(comparator.compare("123.5", "123.50")).isLessThan(0); + assertThat(comparator.compare("123.0005", "123.00050")).isLessThan(0); } @Test