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.
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.
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.
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.
The secret of how to be miserable is to constantly expect things are going to happen the way that they are "supposed" to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there's being a willfully-blind idiot, which virtually guarantees it.