Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting a row in a two deminsional array  RSS feed

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been racking my brain at a particular problem with sorting a certain row of integers in a two dimensional array. I need to sort them in ascending order, as this is part of my final project in my Intro. to Java Programming class.

Ok, the problem is that I have to receive input of 8 students via JOptionPane. There are 6 quizzes that each student receives a grade for, and the numerical assignment is done using Math.random. So the array is,

I'm supposed to write the program so that it can receive user input on which quiz to sort. It's sorted in ascending order, so the names (a seperate array in itself) have to correspondingly change order also.
Output format:

This is a sorted list for test 4
Sach 0
Ami 20
Zack 22
Hum 23
Dum 80

My instructor never showed us how to sort arrays with the predefined class that's included in the Java Class Library, but rather wrote a method himself,

As you can see, this is only for single dimensional arrays, so I'm at a loss on how I should modify this code to allow for a two dimensional array. Perhaps I shouldn't declare the 8 names in their own array?

This is about as far as I could get to, even after looking on the internet for a bit. Also, I DO know that there's a compile problem with this, but the only way I can solve it is to set the arrays in the swap method as one-dimensional.

I apologize for the lengthy post, but I'm trying to be as specific as possible here. There's no rule on HOW I sort the array, just as long as I can get the program to output the form of what was in the example.
[ November 26, 2005: Message edited by: John Oxford ]
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public static void sorting(String[] name, int[][] score)
String temp;
int tempint=0;
temp = JOptionPane.showInputDialog("Which test would you like sorted?"+
"\nnote: Your entry should be one integer less than the test you want!"+
"\nexample: The test you want is 5; entry = 4");
tempint = Integer.parseInt(temp);

for(int j=score.length; j>=2; j--)
for(int k=0; k<j-1; k++)
swap(score,tempint, k, k+1);
for(int i=0; i<8; i++)

public static void swap(int[][] score,int x,int pos1, int pos2)
int temp;
temp = score[x][pos1];
score[x][pos1] = score[x][pos2];
score[x][pos2] = temp;
Forget this weirdo. You guys wanna see something really neat? I just have to take off my shoe .... (hint: it's a tiny ad)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!