Sort TreeMap

B

Ben

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.
 
T

Thomas Weidenfeller

Ben said:
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
 
S

Simon

Ben said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top