• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorting a 2D array

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic