From a59189e6c8ce40784e714cfd24ed9d740c8ef49a Mon Sep 17 00:00:00 2001 From: saharNooby Date: Thu, 26 Aug 2021 16:56:13 +0500 Subject: [PATCH 1/2] Add non-ASCII digit support --- src/com/github/jagobagascon/NaturalSort.java | 14 +++++++------- test/com/github/jagobagascon/NaturalSortTest.java | 10 ++++++++++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/com/github/jagobagascon/NaturalSort.java b/src/com/github/jagobagascon/NaturalSort.java index 48c663d..98d85bf 100644 --- a/src/com/github/jagobagascon/NaturalSort.java +++ b/src/com/github/jagobagascon/NaturalSort.java @@ -118,21 +118,21 @@ private static int compareNumericStrings(int from, String n1, int n1Len, String // Compare strings from right to left for (int i = 1; i <= maxLen; i++) { - // Assume the char is a '0' in case the string is shorter - char c1 = '0'; - char c2 = '0'; + // Assume the digit is a 0 in case the string is shorter + int d1 = 0; + int d2 = 0; if (i <= n1Len) { - c1 = n1.charAt(from + n1Len - i); + d1 = Character.digit(n1.charAt(from + n1Len - i), 10); } if (i <= n2Len) { - c2 = n2.charAt(from + n2Len - i); + d2 = Character.digit(n2.charAt(from + n2Len - i), 10); } // Only update res if they are different - if (c1 != c2) { - res = c1 - c2; // Char comparison is fine as 0 < 1 < ... < 9 + if (d1 != d2) { + res = d1 - d2; } } diff --git a/test/com/github/jagobagascon/NaturalSortTest.java b/test/com/github/jagobagascon/NaturalSortTest.java index 935629b..d9bf8a7 100644 --- a/test/com/github/jagobagascon/NaturalSortTest.java +++ b/test/com/github/jagobagascon/NaturalSortTest.java @@ -70,6 +70,16 @@ public void testGeorgianAlphabetCaseInsensitive() { Assert.assertEquals(sign(capitalDottedI.compareTo(smallDotlessI)), sign(NaturalSort.naturalSortComparator.compare(capitalDottedI, smallDotlessI))); } + @Test + public void testNonAsciiDigits() { + String fullWidthDigitOne = "1"; + String fullWidthDigitFive = "5"; + String fullWidthDigitSix = "6"; + + testSort(Arrays.asList(fullWidthDigitOne, "2", fullWidthDigitFive, "9", + fullWidthDigitOne + fullWidthDigitFive, "1" + fullWidthDigitSix, fullWidthDigitOne + "7")); + } + private void testSort(List expectedResult) { testSort(expectedResult, NaturalSort.naturalSortComparator); } From 073d2508f2f1384c5f0f818794c1e519113ffab0 Mon Sep 17 00:00:00 2001 From: saharNooby Date: Thu, 26 Aug 2021 19:25:57 +0500 Subject: [PATCH 2/2] Add Unicode digits support note to README --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 4771c7f..26af20b 100644 --- a/README.md +++ b/README.md @@ -31,3 +31,7 @@ public void sortNaturalIgnoreCase(List myList) { myList.sort(NaturalSort.naturalSortIgnoreCaseComparator); } ``` + +## Notes + +The library recognizes non-ASCII decimal digits when comparing numbers (for example, Thai digits or full-width digits). Numbers consisting of mixed digit sets or languages are compared as a whole. See [this discussion](https://github.com/jagobagascon/Natural-Sorting-for-Java/issues/9) for more info.