• 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
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

Sorted keys in a Map

 
Ranch Hand
Posts: 434
  • 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: 78653
374
  • 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: 3063
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: 434
  • 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: 78653
374
  • 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: 27478
195
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: 3063
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.
 
It's a tiny ad only because the water is so cold.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic