File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Sorting a set of arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sorting a set of arrays" Watch "Sorting a set of arrays" New topic
Author

Sorting a set of arrays

sebastian graper
Greenhorn

Joined: Oct 23, 2007
Posts: 3
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


"If it sounds good...it is good" - Duke Ellington
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Use a SortedMap (e.g. TreeMap) instead of arrays.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Bridget Kennedy
Ranch Hand

Joined: Nov 30, 2004
Posts: 84
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

Joined: Oct 23, 2007
Posts: 3
I really appreciate the feedback. I'll give your suggestions a try and let you know how it goes - Sebastian
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
sebastian graper
Greenhorn

Joined: Oct 23, 2007
Posts: 3
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
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

descendingMap is a method specified by the NavigableMap interface, not Map, and not even SortedMap. Therefore, declare your map as being a NavigableMap instead.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
 
subject: Sorting a set of arrays
 
Similar Threads
Explanation...
2 dimensional array problem
Printing in Array in Ascending order
Adding elements of two arrays
2 dim array