| Author |
Sort key value pair by ordered array of keys
|
Jacob Fenwick
Ranch Hand
Joined: Apr 28, 2006
Posts: 55
|
|
I have an unsorted, two dimensional array that contains a key value pair. I also have a sorted one dimensional array that contains the keys in their order. Like so: Now, the ordering in the sorted array is probably NOT a natural ordering. I want to sort the unsorted 2d array based on the sorted 1d array. Is there a way built into java to do this? I'm researching using a Collection with a Comparator. I'm not knowledgable enough to immediately have a solution, perhaps someone else has dealt with this before?
|
 |
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
|
|
|
It might be easier if you use a HashMap.
|
 |
Jacob Fenwick
Ranch Hand
Joined: Apr 28, 2006
Posts: 55
|
|
That's true, I suppose I should have explained it in terms of a hashmap to cut down on wording. A more clear question is: Is there a way to get an ordered entrySet from a hashmap, where the ordering is based on some non-natural ordering of the keySet? [ October 12, 2006: Message edited by: Jacob Fenwick ]
|
 |
Jacob Fenwick
Ranch Hand
Joined: Apr 28, 2006
Posts: 55
|
|
|
Nevermind, someone figured it out... If it's in a hashmap, and I have the list of ids, just loop through the list of ids and use those to pull the correct value out of the hashmap.
|
 |
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
|
|
|
I believe you should be able to do that by creating a Comparator whose type is the type of the keys.
|
 |
 |
|
|
subject: Sort key value pair by ordered array of keys
|
|
|