Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Sort TreeMap

Reply
Thread Tools

Sort TreeMap

 
 
Ben
Guest
Posts: n/a
 
      04-27-2006
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.

 
Reply With Quote
 
 
 
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      04-27-2006
Ben wrote:
> 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

--
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/...g/java/gui/faq
http://www.uni-giessen.de/faq/archiv....java.gui.faq/
 
Reply With Quote
 
 
 
 
Simon
Guest
Posts: n/a
 
      04-27-2006
Ben wrote:
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(newbie) converting a hashmap to a treemap Miguel Angel Java 9 04-15-2004 01:28 PM
Help with sorting values in a TreeMap Wendy S Java 4 02-19-2004 07:17 AM
TreeMap problem... Harry Java 3 09-23-2003 11:02 AM
Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. Navin ASP General 1 09-09-2003 07:16 AM
HashMap vs TreeMap Ahmed Moustafa Java 2 08-10-2003 03:31 AM



Advertisments