Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Sorted keys in a Map

 
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want the keys to be sorted in ascending order. So I need a way to name them so it will show up this whey when I go to access the Map. I tried putting them in ascending order using a number. But when you run this, the order is wrong.

I am using this to populate a multi select combo box. Option entries can be added and removed based on other element entries, so I need to insure all the keys are unique and there origin is identifiable. So I am using the combo box ID and option ID to identify the entry. Example: select2Options1 = Combo box 2, Option Group 1.


This is a contrived example:

 
Marshal
Posts: 75682
354
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your keys are Strings so they are not sorted by number order but by what is jocularly called asciibetical order. 19a does not come before 20a but before 1a. Try 000001select2Optional instead. You can get that maybe most easily with the String#format method.
 
Sheriff
Posts: 3036
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would be easier if you could just use an Integer key. I know; you said you need the name of the selector and option group, but do you, really?

Another option is passing in your own Comparator class to the TreeMap. I know there are "smart" string comparators you could find that would compare strings with numbers in them in a more consistent way, and if you can't find one already built, it's not hard to write one yourself. Padding the number as Campbell suggested seems simpler in this case, but keep custom Comparators in mind. They can be a useful tool.
 
M Burke
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I need the keys the way they are. I tried this and it works.
 
Campbell Ritchie
Marshal
Posts: 75682
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can probably reduce that compare method toWhich will sort 19 before 2, orYou can also use a regular expression for digits to find the number, or indexOf("select").
 
Saloon Keeper
Posts: 25466
180
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use a TreeMap. It has has an ordering to its keys.

I am a little worried about all you guys understanding what collating sequences are.

There are 2 options if you don't care what the code is.

1. Create a TreeMap<Integer, String> to hold your key/value pairs and increment an integer counter. Since the natural ordering of integers places 9 before 10, unlike text-based ordering where "1", "10", "11", "2",,, "9" is the rule, that's sufficient.

2. Alternatively, look at the Java formatting resources such as MessageFormat. You can build character strings in the form "001", "002", ... "009", "010", and they, too will collate properly, thanks to the leading zeroes.
 
Greg Charles
Sheriff
Posts: 3036
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For Campbell's suggestion, just the basic String.format() could do the job. You could change your loop to:



The keys would sort correctly without needing a custom Comparator, and probably marginally faster too.
 
I am Arthur, King of the Britons. And this is a tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic