I'm a newbie to Java and have a simple question regarding the compareTo() method.
When using the code
String Name1 = "alan";
System.out.println(Name1.compareTo("a"));
String Name2 = "bob";
System.out.println(Name2.compareTo("a"));
String Name3 = "carl";
System.out.println(Name3.compareTo("a"));
I would expect that the output would be 0 1 2 however, instead I'm getting 3 1 2
I know it may seem simple but I'm genuinely puzzled. I would have thought that the letter a in 'alan' would be compared with 'a' and thus result in 0. If someone could just explain I'd be highly appreciative.
I was under the impression that compareTo only took the first letter of 'alan' into equation as it seemed to have done so with 'bob' and 'carl'. If I wanted to only use the first letter, how would I go about this? My intention is to order these names in alphabetical order.