How do I sort an ArrayList lexicographically?

Jake picture Jake · Jun 8, 2010 · Viewed 34.9k times · Source

I am trying to sort an ArrayList of Strings that represent card values. So, some cards contain letters ("King") and some contain Strings containing only a number ("7"). I know to use Collections.sort, but it only sorts Strings that contain letters. How do I get the ArrayList to be sorted by number as well as alphabetically?

Edit: Sorry, I must not have been paying much attention when I looked at the sorting. The sort works correctly, I must have just been thrown off by the fact that a 10 will come before a 2. Thanks

Answer

Jon Skeet picture Jon Skeet · Jun 8, 2010

No, Collections.sort will sort everything, using an Unicode ordinal lexicographic comparison as that's the behaviour of String.compareTo. "7" will come before "King", and "10" will come before "2".