2

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?

BSMP
  • 4,596
  • 8
  • 33
  • 44
NikS
  • 139
  • 2
  • 11

3 Answers3

2

try this , this is working as per your need, in this code i not using Enclose hyphen('-') between single quotes.

public void orderList() {
    String str1 = "test-2014";
    String str2 = "test195519-9022c72bc161";
    String str3 = "test200101-ee4d99b1492c";
    String str4 = "test212941-884e3f03fe1e";
    //List<String> list = List.newArrayList();
ArrayList<String> list = new ArrayList<String>();
    list.add(str3);
    list.add(str2);
    list.add(str1);
    list.add(str4);

    System.out.println("List before ordering = " + list);
Collections.sort(list);
    System.out.println("List after ordering = " + list);
}

output

List before ordering = [test200101-ee4d99b1492c, test195519-9022c72bc161, test-2014, test212941-884e3f03fe1e]
List after ordering = [test-2014, test195519-9022c72bc161, test200101-ee4d99b1492c, test212941-884e3f03fe1e]
BUILD SUCCESSFUL (total time: 0 seconds)
swapnil7
  • 808
  • 1
  • 9
  • 22
  • Thanks a lot. **Collections.sort(list)** works perfectly. Even with **List list = List.newArrayList();** Just want to know a bit more about how collator works. – NikS Feb 25 '14 at 14:22
1

Your code:

Collator collator = Collator.getInstance();
Collections.sort(list, collator);

In this code you get a natural language collator but we don't know what the ordering rules are because we do not know the locale of your JVM.

Consider this code:

import java.text.*;
import java.util.*;

public class Order {
  public static void main(String[] args) throws ParseException {
    List<String> list = Arrays.asList("a", "A", "\u00E6", "z", "Z", "1", "-");

    Collections.sort(list);
    print("Natural", list);

    Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
    print("Case insensitive", list);

    Collator norwegian = Collator.getInstance(new Locale("nb_NO"));
    norwegian.setStrength(Collator.PRIMARY);
    Collections.sort(list, norwegian);
    print("Localized natural language rules (Norwegian)", list);

    Collator custom = new RuleBasedCollator("< a< A< z< Z< '-'< 1");
    Collections.sort(list, custom);
    print("Custom", list);
  }

  private static void print(String what, Object value) {
    System.out.println(what);
    System.out.println(value);
  }
}

We have four types of collation here:

  1. Natural: using the 16-bit unsigned numeric values of the chars
  2. A locale-insensitive collator applying some rules for English
  3. A natural language collator, here using the rules for Norwegian with only primary differences - see here for the Java 7 locale support list
  4. A collator based on some custom rules I made up

Here is the output:

Natural
[-, 1, A, Z, a, z, æ]
Case insensitive
[-, 1, A, a, Z, z, æ]
Localized natural language rules (Norwegian)
[-, 1, A, a, æ, Z, z]
Custom
[a, A, z, Z, -, 1, æ]

It is for the last collator, the RuleBasedCollator, that the answer for the other question was discussing escaping the hyphen.

McDowell
  • 107,573
  • 31
  • 204
  • 267
0

You will have to write your own compare() or compareTo() method. By default, Collections.sort() sorts the collection using Natural Order and ASCII value of hyphen is 45 and that of 0 is 48.. I donno why you are getting that order.You should be getting the order you are expecting.

Example :

public static void main(String[] args) {
    ArrayList<String> l = new ArrayList<String>();
    l.add("test123");
    l.add("test-100");
    l.add("test567");
    System.out.println(l);
    Collections.sort(l);
    System.out.println(l);

}

O/P : 
[test123, test-100, test567]
[test-100, test123, test567]
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • Collections.sort() works. I am getting the order that I mentioned because of specificity of how Collator works. – NikS Feb 25 '14 at 21:02