This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Sorting a set of arrays

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two arrays; one is defined as integer and the other is String. I want to sort the int array in descending order but still maintain the occurence relationship between the two tables. For example, given:

occurence arr1 arr2
1 8 cat
2 10 dog
3 2 fish
4 7 horse

I would like to produce:

occurence arr1 arr2
1 10 dog
2 8 cat
3 7 horse
4 2 fish

I could use a prewritten java sort method for arr1 but I'm not sure how to preserve the occurence relationship between the content in both arrays. Any suggestions would be greatly appreciated.

thank you,
Seb
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a SortedMap (e.g. TreeMap) instead of arrays.
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's probably an easier way, but this is how I would do it.

Presuming there is a one-to-one correspondence between the Integer and the String, you can store the data in a HashMap<Integer,String>. This establishes a key,value association between the two items. After that, you can sort the HashMap.keySet and use the sorted set to retrieve the String values from the HashMap in the desired order.

P.S. Or you can just make things easy on yourself and take Peter's excellent suggestion.
[ October 23, 2007: Message edited by: Bridget Kennedy ]
 
sebastian graper
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really appreciate the feedback. I'll give your suggestions a try and let you know how it goes - Sebastian
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that most of the time when you find yourself with "parallel arrays" like this, wanting to sort them together and all, that's a very strong hint that there's a little class waiting to be born -- in this case, a Bean-like class with a String and an int as members, that implements Comparable.
 
sebastian graper
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I loaded both a hashmap and a treemap with my array values. I can iterate through and print the occurences in ascending order but I'm still struggling with presenting them in descending order which is what I need.

I found a method called "descendingMap()" java 1.6.0 in the TreeMap class that seems to fit the bill but I can't get this to compile clean.

Map sortedMap = new TreeMap(m);
/*moved my array data to a hashmap "m"then created a TreeMap as a test. Final code will directly populate the TreeMap */

sortedMap.descendingMap(); /* does not recognize this method but it is listed in the java API 1.6.0 doc*/


System.out.println("Retriving all values from the TreeMap");

iterator = sortedMap.entrySet().iterator();

while( iterator. hasNext() ){

System.out.println( iterator.next() );

Any feedback is appreciated.

Thank,
Seb
 
Sheriff
Posts: 21775
103
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
descendingMap is a method specified by the NavigableMap interface, not Map, and not even SortedMap. Therefore, declare your map as being a NavigableMap instead.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!