# Sorting an two arrays together

Page 1 of 1

## 3 Replies - 8910 Views - Last Post: 02 February 2011 - 05:45 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=213666&amp;s=922a8b3a8fa6c3615585a082fa185af2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Pharmicyst

Reputation: 0
• Posts: 1
• Joined: 02-February 11

# Sorting an two arrays together

Posted 02 February 2011 - 04:15 PM

I'm having trouble sorting two arrays.

My assignment is this:
"Write a GradeSort class according to the attached specification. The sort method in this class will sort an array of grades that is passed to it in descending order. The array of names should be re-ordered according to the re-ordering of the grades array.

HINT: The sort method can use the code from either InsertionSort or SelectionSort to sort the grades array. Whenever, the sort routine exchanges elements of the grades array, it should also exchange the corresponding elements of the names array,

Once you have the GradeSort class working properly, write your own main program that reads at least 4 grades and 4 names from the user. It should then sort these values, using the GradeSort class and then print the names and grades. "

I wasn't familar with InsertionSort or SelectionSort, so I tried to do a bubble sort and sort the arrays at the same time, however whenever our test program throws this at it:
```int[] scores5 = { 45, 17, 63, 39, 71 };
String[] names5 = { "Dave", "Alan", "Glenda", "Betty", "Robert" };
```

It doesn't match this:
```			int[] sorted5 = { 71, 63, 45, 39, 17 };
String[] sortedNames5 = { "Robert", "Glenda", "Dave", "Betty",
"Alan" };
```

My code:
```import java.lang.reflect.Array;
import java.util.*;

private static String[] names;

public void sort(String[] names, int[] grade){
if (names.length == 0)
throw new IllegalArgumentException();
throw new IllegalArgumentException();
throw new IllegalArgumentException ("Must be same size");

for(int j=0;  j < grade.length -1;  j++ )
{
{
int temp;
String tempname;
tempname = names[j];
names[j] = names[j+1];
names[j+1] = tempname;

}
}
}
}
```

I'm new here and just looking for some help! Thanks in advance.

Is This A Good Question/Topic? 0

## Replies To: Sorting an two arrays together

### #2 japanir

• jaVanir

Reputation: 1014
• Posts: 3,025
• Joined: 20-August 09

## Re: Sorting an two arrays together

Posted 02 February 2011 - 04:26 PM

Bubble sort should have 2 for loops.
Search the site for bubble sort, insertion sort (and any other sort method actually) you'll find plenty of tutorials and code snippets here

here is the first snippet I found:
http://www.dreaminco.../snippet513.htm
feel free to search for more

### #3 n8wxs

• --... ...-- -.. . -. ---.. .-- -..- ...

Reputation: 972
• Posts: 3,878
• Joined: 07-January 08

## Re: Sorting an two arrays together

Posted 02 February 2011 - 04:46 PM

Here's another example.

### #4 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12126
• Posts: 45,090
• Joined: 27-December 08

## Re: Sorting an two arrays together

Posted 02 February 2011 - 05:45 PM

Parallel arrays aren't good practice. Design a Student class instead and store a Student[]. For more information on class design, check out my tutorial Moving Away From Parallel Arrays.

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }