Hi,
I have a TreeMap with the keys being numbers stored as Strings. I have
made it sorted it, however it sorts it as 1, 10, 11, etc and not 1, 2,
3. What is the simplest way to sort it be arranged as 1, 2, 3.
Thanks in advance.
Ben.
Thomas Weidenfeller - 27 Apr 2006 12:24 GMT
> Hi,
>
> I have a TreeMap with the keys being numbers stored as Strings. I have
> made it sorted it, however it sorts it as 1, 10, 11, etc and not 1, 2,
> 3. What is the simplest way to sort it be arranged as 1, 2, 3.
Don't store the numbers as strings, but as numbers. Alternatively, waste
some time, write your own Comparator and use that in the constructor.
/Thomas

Signature
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
Simon - 27 Apr 2006 12:26 GMT
> I have a TreeMap with the keys being numbers stored as Strings. I have
> made it sorted it, however it sorts it as 1, 10, 11, etc and not 1, 2,
> 3. What is the simplest way to sort it be arranged as 1, 2, 3.
The best solution would probably be to use Integers instead of the Strings as
the keys. However, if you don't want that, you can pass a Comparator to the
constructor of TreeMap that takes care of the ordering.
Cheers,
Simon