aspose file tools*
The moose likes Beginning Java and the fly likes Sorting a 2D array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sorting a 2D array" Watch "Sorting a 2D array" New topic
Author

Sorting a 2D array

Kirk Somero
Greenhorn

Joined: Nov 05, 2004
Posts: 3
I've got an assignment for my Java programming class and I'm having a heck of a time getting it to work.

The assignment is to have a 2d array which is composed of exam #'s for rows, students names for columns, and grades for the data. We need to be able to sort the grades from highest to lowest for each individual student which means we need to move the row labels to correspond with those tests
ie
...........John
Exam 1...87
Exam 2...62
Exam 3...92

Needs to become

...........John
Exam 3...92
Exam 1...87
Exam 2...62

We also need to be able to sort the columns by grades in decending order for the entire group of students

...........Steve....Cory....Adam
Exam 1...87.......92......53

Needs to become
...........Cory.....Steve...Adam
Exam 1...92.......87......53


I can sort all the data, it's moving the labels that I'm having an extreme amount of difficulty with. Any help would be greatly appreciated.
Thanks
Kirk

[ November 05, 2004: Message edited by: Kirk Somero ]
[ November 05, 2004: Message edited by: Kirk Somero ]
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
A modification of a tables column names is basically a modification of a table ( as opposed to a modification of table data)

If you intend to modify your JTable, then you will have to invoke the fireTableChanged() method of your model.
Kirk Somero
Greenhorn

Joined: Nov 05, 2004
Posts: 3
Oh, I should have clarified. We need to make this table by using an array, not by way of a JTable.
The only way I can think of doing it is by having 3 seperate arrays, one for the data, one for the column labels and one for the row labels and try to adjust them individually whenever they are sorted; I'm just not 100% sure how that would work.
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
I guess this is one of those obscure programming assignments whose only goal is to get you more comfortable with the language and various data structures.


There is not going to be an easy approach to this solution.

If you are doing it with three arrays, then that's probably as good an approach as any.

If you can't use a JTable, probably the next best approach would be a class that more logically handle the data for you. But that approach is probably not suited for a learning environment.

Anyway, managing three arrays will give you all you can handle.

Good luck.
Jayesh Lalwani
Ranch Hand

Joined: Nov 05, 2004
Posts: 502
Cant you have an array for each exam? then you can sort each of the exam arrays. So, you would have something like this

class ExamResults
{
Object[][] results;
String examName;
public void sort()
{
//sort your array
}

}

ExamResults[] allResults;
for(all elements in allresults)
allResults.sort();
Kirk Somero
Greenhorn

Joined: Nov 05, 2004
Posts: 3
That could work, but I also need to be able to sort the students in alphabetical order while retaining the exam scores in order. I don't think it's possible...I think my prof is just screwing around with us...
Jayesh Lalwani
Ranch Hand

Joined: Nov 05, 2004
Posts: 502
Ok, then you can a have a 2D array with one row for each exam' result and the columns being exam name, student name and grade. Then if you want to show the result of each exam sorted by grade, then you sort by exam AND grade. When you want to show the result in alphabetical order, you sort by student
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Sorting a 2D array