Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Arranging the element in TreeMap  RSS feed

 
mohan kumar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
i am my work area i am facing a problem like we need to show the month in a drop down with the options order like :

CurrentMonth
Apr-2007
Mar-2007
Feb-2007

For this i am using TreeMap.
when i run the code i am getting in other order like:

Apr-2007
CurrentMonth
Feb-2007
Mar-2007

Can somebody help me in this issue.Waiting for your solution.

Thanks
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By default, Strings are sorted alphabetically.

You'll need to write your own Comparator to order them chronologically.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"leeboo,"

Welcome to JavaRanch!

Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names.

You can edit your display name here. Thank you for your prompt attention!

-Marc
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using a TreeMap and not just a List?
 
mohan kumar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using a TreeMap and not just a List?

in map i will get the key of what the user selected and i can proceed with the key.i am taking key for the further manipulation.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider using a LinkedHashMap and just add the items in the order you want them to be displayed.
 
Sidd Kulk
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the insertion order is not Important, then probably HashMap wud be the best choice.

Sid
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!