java - Sorting of Strings using collections.sort() method -


as per documentation: implementation dumps specified list array, sorts array, , iterates on list resetting each element corresponding position in array

given program below, not able understand sorting how internally jvm judges letter 'a' smaller or bigger letter 'a'? string, letters won't assumed in ascii value how 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);         (string s : strings)          {          system.out.print(s + " "); //prints aaaa aaa aaa aaaa          }     } } 

also tried debugging code created new doubt me: length of array turned out 4 rather 3 collections.sort included in length

the "natural ordering" collections.sort refers 1 specified comparable -- string implements, , defines 1 method, compareto. so, answer in definition of string.compareto. documentation states:

compares 2 strings lexicographically. comparison based on unicode value of each character in strings.

lexicographical ordering means dictionary ordering. essentially, order each letter alphabetically far go, if you're still tied when either word runs out of letters, shorter word goes first.

unicode numerical value each character has. there's great introductory post here (it's not short, job of walking through not unicode is, why exists).