# Why is the Treemap output like this?

Kedar Pethe
Ranch Hand
Posts: 39

Output - 1,11,4,9,

If TreeMap sorts in ascending order and String implements Comparable implicitly then why is the output like this??
Shouldn't it be in ascending order - 1,4,9,11

Pritish Chakraborty
Ranch Hand
Posts: 91
Strings follow a natural sorting order, remember that this a String we're dealing with, not actual numbers!

Compare the first positions of all the Strings first. If two first positions are the same, compare by the next position and so on. (Kind of how we find words in the dictionary)

1 comes first, so 1 and then 11 (1 before 11 because there is nothing at the second position there).

Then come 4 and 9, as expected.

Also remember that spaces sort before characters, and uppercase characters sort before lowercase ones.

When dealing with only one set of characters, follow the alphabetical order.

Hope this helps

Nitish Bangera
Ranch Hand
Posts: 537
Its just as Pritish said. The hashCode for strings are defined in that way. You can verify it looking at the String's implementation of the hashCode.

Kedar Pethe
Ranch Hand
Posts: 39
Pritish Chakraborty wrote:Strings follow a natural sorting order, remember that this a String we're dealing with, not actual numbers!

Compare the first positions of all the Strings first. If two first positions are the same, compare by the next position and so on. (Kind of how we find words in the dictionary)

1 comes first, so 1 and then 11 (1 before 11 because there is nothing at the second position there).

Then come 4 and 9, as expected.

Also remember that spaces sort before characters, and uppercase characters sort before lowercase ones.

When dealing with only one set of characters, follow the alphabetical order.

Hope this helps

But my query is if you say that we are not dealing with numbers then how does the jvm know that value 4 comes before 9 (as shown in the output) ?

Also please tell me what do you mean by spaces sort before characters..., does the above sorting sequence and spaces sort before characters thing imply sorting according to their ASCII values??

Paul Clapham
Sheriff
Posts: 21322
32
Kedar Pethe wrote:does the above sorting sequence and spaces sort before characters thing imply sorting according to their ASCII values??

Yes, that's correct. Actually the default sequence for characters follows the Unicode character set, of which ASCII is just the first 128 characters.

Kedar Pethe
Ranch Hand
Posts: 39
Paul Clapham wrote:
Kedar Pethe wrote:does the above sorting sequence and spaces sort before characters thing imply sorting according to their ASCII values??

Yes, that's correct. Actually the default sequence for characters follows the Unicode character set, of which ASCII is just the first 128 characters.

Thanks Paul!!

Nitish Bangera
Ranch Hand
Posts: 537
Paul Clapham wrote:
Kedar Pethe wrote:does the above sorting sequence and spaces sort before characters thing imply sorting according to their ASCII values??

Yes, that's correct. Actually the default sequence for characters follows the Unicode character set, of which ASCII is just the first 128 characters.

I guess this is the reason i pointed to check the hashCode of the String class.