• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sort key value pair by ordered array of keys  RSS feed

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might be easier if you use a HashMap.
 
Jacob Fenwick
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe you should be able to do that by creating a Comparator whose type is the type of the keys.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!