How can I sort the data in a loop from least to greatest if the data gets replaced each time it goes back into the loop?

I have a program that should behave like this. You enter the number of students in a class, lets say 3, for example. So then you enter a student ID, followed by 4 numbers. The first 3 being grades that average out to 80% of the total class grade and the 4th number being what grade the student desires to get after taking the final exam which is 20% of the total class grade.

Example:  
Enter number of students in class: 3  
(Here you would enter the data)  
123456 90 90 90 90  
543216 70 80 80 70  
435366 80 80 80 90  

Then the output SHOULD be:

123456 90 //(The minimum score the student must get on their final to get the grade they   want)  
435366 130  
543216 44  

I got all the math down and it works fine. I enter the information and I get the correct numbers back. The problem I'm having is getting the output to be displayed in order form Least to Greatest. I entered the information using a table with Rows being the number of students in the class and Columns being 5 (ID, grade1, 2, 3, desired grade)

for (int count = 0; count < numberOfStudents(in this case, 3); ++count)  
{  
    StudentID = The first ID I entered (123456);    
    Grade = *a bit of math to solve for this*;  
}  

Now when it enters the loop again, the values for the StudentID and Grade are replaced so I can't compare them to anything to check which is smaller than which. I had an idea of putting the StudentID's into an array then comparing that but then the problem I had was figuring out how to get the corresponding grades to go along with the ID's.

Any Ideas?

Jon Skeet
people
quotationmark

It sounds like really you should create a Student class, which contains their ID, their results and their desired final grade. You can then have a collection of students (e.g. a Student[] or a List<Student> and sort that by the existing results using Collections.sort. (To make the sort compare grades, either make Student implement Comparable<Student>, or write a GradeComparator which implements Comparator<Student>.)

As a general principle, whenever you find yourself with multiple collections, when each element of one collection corresponds to the same element index of others, you should consider encapsulating the data into one type and then a collection of that type. So as another example, instead of having:

String[] addressLines1;
String[] addressLines2;
String[] cities;
String[] zipCodes;
String[] countries;

... you'd have an Address[] or a List<Address>, where the Address class had properties for line 1, line 2, city, zip code and country.

people

See more on this question at Stackoverflow