Sort strings containing numbers, chars and ponctuations in ”human readable order”

49 views Asked by At

Assuming a list has the following entries:

List<String> list = Arrays.asList("A0", "A1", "A2", "A11", "A01", "A001", "A.1", "A-1", 
"a1", "a2", "a 1", "B1", "B2", "0A", "001", "1A", "10A", "10-A", "10.A", "A&B");

I'm using the code bellow to sort my list and obtain the following result :

[001, 0A, 1A, 10A, 10-A, 10.A, A0, A001, A01, A1, A2, A11, A-1, A&B, a 1, a1, a2, a11, B1, B2]

but the actual result is :

[001, 0A, 10A, 10-A, 10.A, 1A, A0, A001, A01, A1, A.1, A-1, A11, A2, A&B, B1, B2, a1, a 1, a2]

Which is not the expected sort order.

list = list.stream()
            .sorted(Comparator.comparing(string -> string.replaceAll("[^\\p{L}0-9]", "")))
            .collect(Collectors.toList());

Are there variants of the sorting algorithm that would

  1. ignore anything that is not a letter or digit (i.e. ignore spaces and punctuation)

  2. sort numbers in ”human order” (ex : Avoid 10 from appearing before 1 and 11 from appearing before 2 etc..)

?

0

There are 0 answers