This question is related to Java case insensitive localized ordering.
I have a similar issue with the following code:
public void orderList() {
String str1 = "test-2014";
String str2 = "test195519-9022c72bc161";
String str3 = "test200101-ee4d99b1492c";
String str4 = "test212941-884e3f03fe1e";
List<String> list = Lists.newArrayList();
list.add(str3);
list.add(str2);
list.add(str1);
list.add(str4);
System.out.println("List before ordering = " + list);
Collator collator = Collator.getInstance();
Collections.sort(list, collator);
System.out.println("List after ordering = " + list);
}
------------- OUTPUT ----------------------------
List before ordering = [test200101-ee4d99b1492c, test195519-9022c72bc161, test-2014, test212941-884e3f03fe1e]
List after ordering = [test195519-9022c72bc161, test200101-ee4d99b1492c, test-2014, test212941-884e3f03fe1e]
I expect the following:
[test-2014, test195519-9022c72bc161, test200101-ee4d99b1492c, test212941-884e3f03fe1e]
According to the accepted answer in the mentioned question, I have to put hyphens in single quotes.
The problem is that the list for sorting usually comes from other methods.
Is there any way to configure Collator to get desired output without editing the list by adding single quotes near hyphen or overriding compare()
method?