*
The moose likes Beginning Java and the fly likes Sorting a row in a two deminsional array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sorting a row in a two deminsional array" Watch "Sorting a row in a two deminsional array" New topic
Author

Sorting a row in a two deminsional array

John Oxford
Greenhorn

Joined: Nov 26, 2005
Posts: 1
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.
Example:
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 ]
Ko Wey
Ranch Hand

Joined: Sep 08, 2003
Posts: 67
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++)
if(score[tempint][k]>score[tempint][k+1])
swap(score,tempint, k, k+1);
for(int i=0; i<8; i++)
System.out.println(score[tempint][i]+"\n");
}



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;
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting a row in a two deminsional array
 
Similar Threads
Ordering of AlphaNumeric No. in ascending Order
How to display a sorted list after sorting.
Load Main class error!!! trying to sort the names inputted in alphabetical order...please help!
sorting
strings