# Sorting Two Dimensional arrays

• 12-09-2010, 09:37 PM
anfielder
Sorting Two Dimensional arrays
I have encountered a problem in sorting two D arrays. Basically, I'm trying to have my arrays sorted out horizontally first and then vertically. After researching, I found a way to sort a list of numbers but having arrays seems to be a pain. Here's my code so far:
Code:

```import java.util.Arrays; import java.util.Scanner; public class TwoDArray {         public static void main(String [] args)         {                 Scanner input = new Scanner(System.in);                 System.out.println("enter the size i: ");                 int k=input.nextInt();                 System.out.println("enter the size of j: ");                 int l=input.nextInt();                 int arr[][] = new int[l][k];                 int i=0, j=0;                 while (i<k)                 {                         while(j<l)                         {                                 System.out.println("arr["+i+"]["+j+"]: ");                                 arr[i][j]=input.nextInt();                                 j+=1;                         }j=0;//end of while j                         i+=1;                 }i=0;//end of while i                 while(i<k)                 {                         while (j<l)                         {                                 System.out.print(arr[i][j]+"\t");                                 j+=1;                         }j=0;                         i+=1;                         System.out.println();                 }i=0;                 double [] lengths = {23,12,32,83,45};                 Arrays.sort(lengths);                 System.out.println(Arrays.toString(lengths));         } }```
For the sorting part, I just put in random numbers for it to work. My issue, is incorporating the arrays [i][j] and sorting it both vertically and horizontally.
I'll appreciate all the input
• 12-09-2010, 10:18 PM
pbrockway2
Quote:

I'm trying to have my arrays sorted out horizontally first and then vertically.

What is the vertical sort supposed to do: sort on the value of the first element of the row? the length of the row? something else? If the first of these how will empty rows be handled?

-----------------

In general Arrays.sort() is good. It can be used to sort all the rows into ascending order. And then can be applied to sort the rows: you have to supply a comparator for that. And that depends on having a specific vertical sort in mind.
• 12-10-2010, 09:17 PM
anfielder
I managed sorting the numbers horizontally using the general formula you provided above. The only problem is with sorting the numbers vertically. I was given the idea of transforming the axis but I've had a few problems.
Check out my code so far

Code:

```import java.util.Arrays; import java.util.Scanner; public class TwoDArray {         public static void main(String [] args)         {                 Scanner input = new Scanner(System.in);                 System.out.println("enter the size i: ");                 int k=input.nextInt();                 System.out.println("enter the size of j: ");                 int l=input.nextInt();                 int arr[][] = new int[l][k];                 int arr2[][] = new int [k][l];                 int i=0, j=0;                 while (i<l)                 {                         while(j<k)                         {                                 System.out.println("arr["+i+"]["+j+"]: ");                                 arr[i][j]=input.nextInt();                                 j+=1;                         }j=0;//end of while j                         i+=1;                 }i=0;//end of while i                 while (i<l)                 {                         Arrays.sort(arr [i]);                         i++;                 }i=0;                 while(i<l)                 {                         while (j<k)                         {                                 System.out.print(arr[i][j]+"\t");                                 j+=1;                         }j=0;                         i+=1;                         System.out.println();                 }i=0;                 arr2[j][i] = arr[i][j];                 while (j<k)                 {                         Arrays.sort(arr [i]);                         i++;                 }i=0;         } }```
• 12-10-2010, 10:14 PM
pbrockway2
I'm still unsure what the vertical sort is supposed to do. I had imagined that it was sorting whole lines up and down. But is your intention to sort elements of a column up and down?

If so your idea of "transforming the axis" - ie transposing the matrix - is the way to go. (The matrix must be rectangular, of course).

The general plan of attack is:

* Sort each row
* Transpose the matrix
* Sort each row again (they are now really the columns)
* Transpose the matrix back again

Code:

`arr2[j][i] = arr[i][j];`

This does very little. Perhaps you mean:

Code:

```for(int row = 0; row < l; row++) {     for(int col = 0; col < k; col++)     {         arr2[col][row] = arr[row][col];     } }```

---------------------------

I also prefer for loops for the sorting:

Code:

```for(int row = 0; row < k; row++) {     Arrays.sort(arr[row]); }```
---------------------------

To keep your thinking straight choose meaningful variable names. I would go for arr and arrT. And perhaps row and col for variables (defined locally within the for loops). And numRows and numCols if you need them in place of k and the nasssty l. Row and column, of course, are relative to arr rather than the transposed arrT.

Just my ideas for names... But whatever you do keep rows and columns straight in your mind.

---------------------------

Ask if the sort-rows/transpose/sort-rows/transpose doesn't make sense (or if you think it's wrong). It seemed to be sort of what you were doing.
• 12-14-2010, 04:36 AM
anfielder
Transposing is still not working. Sorting works only horizontally and not vertically. Tried your suggestion but it still doesn't work
• 12-14-2010, 08:12 AM
pbrockway2
Quote:

Transposing is still not working.

There's not a lot to go on here.

Perhaps you could post some (runnable) code and say what its output/behaviour is?

I'm *still* not sure what you mean by sorting vertically. Is it this:

Code:

```1 4 6 7    1 4 6 7    1 2 3 5 5 3 2 2 --> 2 2 3 5 --> 2 4 6 7 2 6 8 7    2 6 7 8    2 6 7 8```