This content has been marked as final. Show 9 replies
The keys in e.g. a HashMap or Hashtable will never be sorted. Add the keys to e.g. a List and sort the list.
Also, are your keys Strings? Because it looks like it's sorting it lexically instead of numerically (10 before 2).
In your code you dont display any sorting algoritm.
How do you do the actual sorting ?
Use a TreeMap - it has ordered keys. If you want to keep insertion order, use LinkedHashMap.
I did use to insert the map before displaying
however it gave me
Map map = new TreeMap();
1 11 12 2 3 etc
however it gave meSee reply #2. Either store keys as numeric types or instantiate TreeMap with your own [custom Comparator|http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html].
Then I'd guess Kayaman has the right idea - your keys are strings and not integers. You could provide a comparator for the map, one that parses the strings and compares the int values. It would of course be better to use integer keys though. Btw, you really should use typed maps, i.e. TreeMap<Integer, Whatever>.
Do you put Strings in the Map?
Thanks this worked
The keys in e.g. a HashMap or Hashtable will never be sorted. Add the keys to e.g. a List and sort the list. Kaj
Edited by: Preaching on Apr 2, 2008 1:21 PM