aspose file tools
The moose likes Beginning Java and the fly likes Sort key value pair by ordered array of keys Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Sort key value pair by ordered array of keys" Watch "Sort key value pair by ordered array of keys" New topic
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sort key value pair by ordered array of keys
 
Similar Threads
Doubt in binarySearch
Hashtable / HashMap Problem.
sorting a array
question on array and printing
Sorting Maps