views:

1029

answers:

3

Hi, I've a List<String> object that contains country names. How can I sort this list alphabetically?

Thanks.

+10  A: 

Assuming that those are Strings:

 java.util.Collections.sort(listOfCountryNames)
Thilo
If you have country names such as "UK" then that is going to be broken.
Tom Hawtin - tackline
In this case, you can pass in a Comparator as an extra argument. Implementing that Comparator would be the interesting part, though.
Thilo
Implemeting the Comparator = use java.text.Collator.getInstance
Thilo
+2  A: 

Solution with Collections.sort

If you are forced to use that List, or if your program has a structure like

  • Create List
  • Add some country names
  • sort them once
  • never change that list again

then Thilos answer will be the best way to do it. If you combine it with the advice from Tom Hawtin - tackline, you get:

java.util.Collections.sort(listOfCountryNames, Collator.getInstance());

Solution with a TreeSet

If you are free to decide, and if your application might get more complex, then you might change your code to use a TreeSet instead. This kind of collection sorts your entries just when they are inserted. No need to call sort().

Collection<String> countryNames = 
    new TreeSet<String>(comp, Collator.getInstance());
countryNames.add("UK");
countryNames.add("Germany");
countryNames.add("Australia");
// Tada... sorted.

Side note on why I prefer the TreeSet

This has some subtle, but important advantages:

  • It's simply shorter. Only one line shorter, though.
  • Never worry about is this list really sorted right now becaude a TreeSet is always sorted, no matter what you do.
  • You cannot have duplicate entries. Depending on your situation this may be a pro or a con. If you need duplicates, stick to your List.
  • An experienced programmer looks at TreeSet<String> countyNames and instantly knows: this is a sorted collection of Strings without duplicates, and I can be sure that this is true at every moment. So much information in a short declaration.
  • Real performance win in some cases. If you use a List, and insert values very often, and the list may be read between those insertions, then you have to sort the list after every insertion. The set does the same, but does it much faster.

Using the right collection for the right task is a key to write short and bug free code. It's not as demonstrative in this case, because you just save one line. But I've stopped counting how often I see someone using a List when they want to ensure there are no duplictes, and then build that functionality themselves. Or even worse, using two Lists when you really need a Map.

Don't get me wrong: Using Collections.sort is not an error or a flaw. But there are many cases when the TreeSet is much cleaner.

Brian Schimmel
If you have country names such as "UK" then that is going to be broken.
Tom Hawtin - tackline
Fixed it - do I get my upvote back, now? ;)
Brian Schimmel
I don't see any advantage to using TreeSet over sort.
DJClayworth
Well TreeSet avoids accidental duplication (which may be good or bad) and it should be faster (though not by much given the size of input) and it will always be sorted rather than sorted after all the input (unless you sort the list after each insert) which may matter, but probably not. So faster..
TofuBeer
@TofuBeer I was just clarifying this while you were doing it too.
Brian Schimmel
And if you really care about what's faster, have a look at http://stackoverflow.com/questions/168891/is-it-faster-to-sort-a-list-after-inserting-items-or-adding-them-to-a-sorted-list
Brian Schimmel
+1  A: 

Use the two argument for of Collections.sort. You will want a suitable Comparator that treats case appropriate (i.e. does lexical, not UTF16 ordering), such as that obtainable through java.text.Collator.getInstance.

Tom Hawtin - tackline