0

As per the documentation: This implementation dumps the specified list into an array, sorts the array, and iterates over the list resetting each element from the corresponding position in the array

Given the program below, I am not able to understand the sorting as how internally jvm judges that letter 'A' is smaller or bigger than letter 'a'? As this is a string, the letters won't be assumed in ascii value so how the sorting happens?

public class LetterASort {
    public static void main(String[] args) {
        ArrayList<String> strings = new ArrayList();
        strings.add("aAaA");
        strings.add("AaA");
        strings.add("aAa");
        strings.add("AAaa");
        Collections.sort(strings);
        for (String s : strings) 
        { 
        System.out.print(s + " "); //prints AAaa AaA aAa aAaA 
        }
    }
}

Also I tried debugging the code which created a new doubt for me: the length of array turned out to be 4 rather than 3 as collections.sort is included in the length

kittu
  • 6,662
  • 21
  • 91
  • 185
  • 3
    Why do you expect a length of 3? I see 4 elements added to the arraylist – Richard Tingle Apr 28 '15 at 05:43
  • but the length of array index starts from 0 right ? – kittu Apr 28 '15 at 05:45
  • 3
    Yes, but that's fine: a _length_ of 4 means that the _last index_ is 3. The indexes are {0, 1, 2, 3}. There are four numbers there (ie, the length), the largest of which is 3 (ie, the last index). – yshavit Apr 28 '15 at 05:46
  • 2
    @kittu the index starts from zero. The length is not the maximum index though, it is the number of elements in the collection – Richard Tingle Apr 28 '15 at 05:47
  • Don't be confused between length and index. Length is number of elements in list, while index is something which used to point out the location of object in collection. – Vimal Bera Apr 28 '15 at 05:47

2 Answers2

5

The "natural ordering" that Collections.sort refers to is the one specified by Comparable -- which String implements, and which defines only one method, compareTo. So, the answer is in the definition of String.compareTo. Its documentation states:

Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings.

Lexicographical ordering basically means dictionary ordering. Essentially, order each letter alphabetically as far as you go, but if you're still tied when either word runs out of letters, then the shorter word goes first.

Unicode is the numerical value that each character has. There's a great introductory post about it here (it's not short, but it does a good job of walking you through not just what unicode is, but why it exists).

yshavit
  • 42,327
  • 7
  • 87
  • 124
  • Could you please provide me a link if possible. Thanks – kittu Apr 28 '15 at 05:58
  • A link to what? The words "Comparable" and "String.compareTo" are both linked to their respective documentation. I'd be happy to link to whatever else you need, just let me know. – yshavit Apr 28 '15 at 05:59
  • @kittu - [here](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8-b132/java/util/Collections.java#Collections.sort%28java.util.List%29) check the source code and follow the links :) – TheLostMind Apr 28 '15 at 06:02
  • Thanks a lot for your effort yshavit and @TheLostMind for following it up on answers ;) – kittu Apr 28 '15 at 06:06
  • @TheLostMind Just to add up to this: if I was given a question like this in scjp, then do I have to know the unicode values? This question is from scjp book I am preparing – kittu Apr 28 '15 at 06:22
  • @kittu - Nope.. That would be ridiculous. :P – TheLostMind Apr 28 '15 at 06:23
3

String class implements the Comparable interface. When sort happens, compareTo(String) method is called. For more look at the implementation of compareTo(String) method in String class.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
Ramesh-X
  • 4,853
  • 6
  • 46
  • 67