7

what I would like to achieve is to sort a colletion of objects by a string value. However in a locale dependant way using a collator. Due to performance reasons I do not want to use the Collator compare() method (as below in the code) rather the CollationKey class, as the java API states the using a CollationKey is much faster.

But how do I implement the compareTo() method using the CollationKey? As far as I understood it, I have to completely write all the comparison Methods on my own if I will be using a CollationKey. So I will even no longer be able to use the Collections.sort() methods... I am very thankfull for an example that is easy to understand and a the most efficient implementation to sort the Collection of Person objects using a CollationKey.

Thank you!

public class Person implements Comparable<Person> {

String lastname;

public int compareTo(Person person) {
     //This works but it is not the best implementation for a good performance
     Collator instance = Collator.getInstance(Locale.ITALY);
     return instance.compare(lastname, person.lastname);
}
}

...
ArrayList list = new ArrayList();
Person person1 = new Person("foo");
list.add(person1);
Person person2 = new Person("bar");
list.add(person2);
Collections.sort(list);
...
jan
  • 131
  • 2
  • 7

3 Answers3

14
class Person implements Comparable<Person> {

  private static final Collator collator = Collator.getInstance(Locale.ITALY);

  private final String lastname;

  private final CollationKey key;

  Person(String lastname) {
    this.lastname = lastname;
    this.key = collator.getCollationKey(lastname);
  }

  public int compareTo(Person person) {
     return key.compareTo(person.key);
  }

}
erickson
  • 265,237
  • 58
  • 395
  • 493
  • Hello Erickson, thank you very much for the smart solution, I will implement it like this, thanks! – jan Sep 16 '09 at 02:03
0
  1. Create a SortedMap m, where T is the type of the objects you want to sort using CollationKeys. You can use TreeMap as the implementation
  2. For each e element you want to sort, m.put(collator.getCollationKey(e.{getStringYouWantToSortOn}), e);

Iterating over m.values() should yield your objects, sorted by the string you want using CollationKeys.

I believe this is not efficient, but it should work.

Rohit
  • 2,646
  • 6
  • 27
  • 52
alex
  • 5,213
  • 1
  • 24
  • 33
-2

use a Comparator instead of making Person Comparable. your Comparator can take 2 Persion instances and compare them based on some Collator instance. then call

Collections.sort(list, myPersonComparator);
james
  • 1,379
  • 7
  • 6
  • The problem is storing the collationKey in the Comparator so that it's not calculated repeatedly... – alex Sep 14 '09 at 21:49
  • ah, i see the problem. i haven't used Collators at all. i thought the original question was merely addressing continuously reacquiring the Collator instance. – james Sep 16 '09 at 17:53