# Sorting Multiple Parallel Arrays

• 05-10-2010, 01:09 AM
Pyrexkidd
Sorting Multiple Parallel Arrays
So it's end of the year finals time... My EOY the project is to input a file, do a bunch of calculations, sort redo the calculations, or I could do the calculations once and then sort the calculations along with the input. I think since I'm reading the file into an array anyway it's easy enough to print out line by line and do the calculations after each sort.

Ok so it's easy to iterate through the parallel arrays and display the results.

Code:

```for ( i =0 ; i < names.length ; i++){     outFile.print(names[i] + " " + hours[i] + " " + payRate[i] + " ");     outFile.print(calcPay(hours[i], payRate[i]) + " " + calcTax(payRate[i]); }```
Where I am getting stuck is in the sort and print. ie I need to sort an alphabetical list of names and then print out the results sorted.
then i need to sort by hours and do the same thing. normally I would use:
Code:

`Arrays.sort(names);`
but this doesn't sort the other two arrays and I'm stuck with my names not matching pay rate etc.

• 05-10-2010, 03:26 AM
Fubarable
You'd be better off not using parallel arrays, but rather create a class that holds all the information about a single person (name, pay, etc), and create objects for each person (employee?). If you want to make it even better, have the class implement the Comparable interface so that sorting an array of these objects is much easier.
• 05-10-2010, 07:47 AM
m00nchile
If you simply must have your information in parallel arrays (Fubarables solution is more practical, and more in tune with OOP), then you'd need to write your own sorting algorithm (not very difficult), and do the same transformations on both arrays at the same time. An example with bubble sort:
Code:

```int[] values = {...}; String[] names = {...}; void bubble() {   for(int i = 0; i < values.length; i++) {     for(int j = i; j < values.length; j++) {       if(values[i] > values[j]) {         swapValues(i, j);         swapNames(i, j);       }     }   } } void swapValues(int i, int j) {   int temp = values[i];   values[i] = values[j];   values[j] = temp; } void swapNames(int i, int j) {   String temp = names[i];   names[i] = names[j];   names[j] = temp; }```
• 05-11-2010, 07:24 AM
Pyrexkidd
Quote:

Originally Posted by Fubarable
You'd be better off not using parallel arrays, but rather create a class that holds all the information about a single person (name, pay, etc), and create objects for each person (employee?). If you want to make it even better, have the class implement the Comparable interface so that sorting an array of these objects is much easier.

while I agree with you entirely that's not the assignment. this is the teacher who wrote in the notes on my first assignment (it was a add two numbers from the command line type program, to which I used an array list and allowed the addition of multiple numbers) "You don't know how to use this. DO the assignment and turn in again."

however, I am curious about this "Comparable" interface. how would that be implemented? personally I would do something like:
Code:

```public class employee{ private string _name; private double _hoursWorked; private double _payRate;   public static void employee(string name, double hours, double pay){       _name = name;       _hoursWorked = hours;       _payRate = pay;   }   public static string getName(){       return _name;   }   public static double getHours(){       return _hoursWorked;   }   public static double getPay(){       return _payRate;   } } //end employee public class final_project{     public static void main(){           ...           Employee[] castList = new Employee[X];           for ( int i = 0; i < castList.length; i++){                 line = Filereader(INFILE);                 StringTokenizer st = new StringTokenizer(line);                       name = st.NextToken();                       pay = st.NextToken();                       hours = st.NextToken();                 castList[i] = (name, pay, hours);             } //end for }```
so how would I sort my array of employees?

this is what I would think would be the best implementation. an array of a class.

then I could get any values I needed with:
Code:

```... local_name = castList[1].getName; ...```
also can any method inside my final_project {} class access my employee{} class via the public methods? or do I have to pass my array of ojects?
• 05-11-2010, 08:37 AM
Pyrexkidd
so here is my take on the parallel swap with my modification for a 2d data array instead of a 1d array. also I passed the array into the method so the method could modify the array. otherwise the code wouldn't compile.

any thoughts?

Code:

```int[][2] values = {...}; String[] names = {...}; void bubble() {   for(int i = 0; i < values.length; i++) {     for(int j = i; j < values.length; j++) {       if(values[i][1] > values[j][1]) {         swapValues(i, j, values);         swapNames(i, j, names);       }     }   } } void swapValues(int i, int j, int[][] values) {   int temp = values[i][1];   values[i][1] = values[j][1];   values[j][1] = temp;   int temp2 = values[i][2];   values[i][2] = values[j][2];   values[j][2] = temp; } void swapNames(int i, int j, string[] names) {   String temp = names[i];   names[i] = names[j];   names[j] = temp; }```
• 05-11-2010, 08:55 AM
JosAH
Quote:

Originally Posted by Pyrexkidd
so here is my take on the parallel swap with my modification for a 2d data array instead of a 1d array. also I passed the array into the method so the method could modify the array. otherwise the code wouldn't compile.

any thoughts?

Yep, as soon as you run that code you'll get an ArrayIndexOutOfBoundsException. Your values[][] array has two columns. Array indexing starts at zero so the column indexes are 0 and 1.

kind regards,

Jos
• 05-12-2010, 04:01 AM
Pyrexkidd
Quote:

Yep, as soon as you run that code you'll get an ArrayIndexOutOfBoundsException. Your values[][] array has two columns. Array indexing starts at zero so the column indexes are 0 and 1.

kind regards,

Jos
haha... shoulda seen that one coming... it's only funny cause it got me stuck for close to five minutes... if I've made that mistake once...

but how about implementing this code with objects rather than parallel arrays?

I can create objects using that class. but I'm not sure how to sort an array of objects.
• 05-12-2010, 07:34 AM
JosAH
Quote:

Originally Posted by Pyrexkidd
I can create objects using that class. but I'm not sure how to sort an array of objects.

Suppose you have two objects, both instantiations of a same class T; also suppose you can say: a.comparteTo(b) and the result is negative if the relation a < b is true; the result is positive if the relation a > b is true and the result is zero if a == b is true. That is exaclty what the Comparable interface is about and it is possible to sort a list of Comparables because the relation defines an ordering. All you have to do is implement that interface and apply one of the ready made sorting algorithms (Collections, Arrays).

kind regards,

Jos