• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sort key value pair by ordered array of keys

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic