1

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

5
  • 3
    Why do you expect a length of 3? I see 4 elements added to the arraylist Commented Apr 28, 2015 at 5:43
  • but the length of array index starts from 0 right ? Commented Apr 28, 2015 at 5: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). Commented Apr 28, 2015 at 5: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 Commented Apr 28, 2015 at 5: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. Commented Apr 28, 2015 at 5:47

2 Answers 2

6

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).

Sign up to request clarification or add additional context in comments.

6 Comments

Could you please provide me a link if possible. Thanks
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.
@kittu - here check the source code and follow the links :)
Thanks a lot for your effort yshavit and @TheLostMind for following it up on answers ;)
@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
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.