2

So I am allowing the user to enter a 3 by 3 array and then the code should take the array and sort the integers based by columns. Such as:
[15, 87, 37,
55, 5, 22,
30, 12, 40]

becomes
[15, 5, 22,
30, 12, 37,
55, 87, 40]

Here is my method that seems to not work for the columns. It is instead sorting by row?

public static double[][] sortColumns(double[][] array)
{
double[][] sorted = array;
  for(int x = 0; x < 3; x++)
  {
     Arrays.sort(sorted[x]);
  } //end loops
  return sorted;
} //end sortRows

I am not very familiar with coding so I do not 100% understand comparators that I saw some people use instead of .sort for this. If someone could be nice enough to help me out to solve this problem that would be great. Thank you.

1
  • Sorting by column suggests that the data is logically grouped by column, not by row. So the easiest way would be to fill it column-wise instead of row-wise (only your way of thinking says that the first dimension are the rows, it could be column as well). Could you tell us something about the data you store? Commented Nov 19, 2015 at 18:05

5 Answers 5

1

How about you transpose it first, then sort the component arrays and then transpose back?

Sign up to request clarification or add additional context in comments.

1 Comment

What do you mean by this? Like I said I am still new to programming.. Are you saying to make it so i change the columns to rows in the sorted array and then sort by rows then change it back?
1

Basically what you're asking to do is sort every array by the same index of every other array, which isn't something that is simply built-in to Java. You must transpose the array. This solution allows for future operations you may need that operate on matrices. Basically, this means:

[row][column] => [column][row] 

In this form, the arrays can be sorted one by one in the way that you want, and then transposed back into the original form to give you the expected results.

You will need to write code for this. Alternatively, you could look for a library that already does transposing. There are many matrix libraries out there such as JAMA.

Comments

0

Assuming the user is always inputting a 3 by 3, when you get user input just store your array differently so it is easier to sort. Store the matrix based on columns instead of rows. You can do something like this:

Scanner scan = new Scanner(System.in);
int[] col1 = new int[3];
int[] col2 = new int[3];
int[] col3 = new int[3];
for (int i=0; i<3; i++){ //Store based on column not row
    col1[i] = scan.nextInt();
    col2[i] = scan.nextInt();
    col3[i] = scan.nextInt();
}
int[][] matrix = new int[3][3];
matrix[0] = col1;
matrix[1] = col2;
matrix[2] = col3;
for (int i=0; i<3; i++){  //Sort columns
    Arrays.sort(matrix[i]);
}
//The following code is used to print your array properly by rows instead of columns 
for (int i=0; i<3; i++){
    for (int j=0; j<3; j++){
        System.out.print(matrix[j][i]+" ");
    }
    System.out.println();
}

After you sort by columns, you can transpose the matrix back to storing by rows so it is easier to print if you would like.

If you want to have the user set the size of the matrix to make it dynamic you can do something like this:

Scanner scan = new Scanner(System.in);
int N = 3;  //Size of matrix, You can have user input this as well. 

int[][] matrix = new int[N][N];
for (int n=0; n<N; n++){  //Initialize Columns
    for (int column=0; column<N; column++){
        matrix[column][n] = scan.nextInt();  //Store based on column
    }
}
for (int i=0; i<N; i++){  //Sort columns
    Arrays.sort(matrix[i]);
}
//The following code is used to print your array properly by rows instead of columns 
for (int i=0; i<N; i++){
    for (int j=0; j<N; j++){
        System.out.print(matrix[j][i]+" ");
    }
    System.out.println();
}

8 Comments

This solution does not scale.
@Hypino I didn't write it to scale. I assumed the user was always inputting a 3 by 3 based off of what OP stated.
@Hypino you can make it scale pretty easily. What gonzo is saying is "store it in colum-major instead of row-major". It is just a way of thinking that the first dimensions are "rows".
@Turing85 yup i agree.
@Turing85 I encourage you to answer with a scalable solution, in that case.
|
0

Here is a solution that would work for your cas. To sort by column, I retrieve the values by column and then store those values into array list and sort it and store the sorted values back into the column (reversing the loop).

public static void main(String[] args) {
        int[][] x = new int[][]{{15,87,37},{55,5,22},{30,12,40}};

        ArrayList<Integer> a = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                a.add(x[j][i]);
            }
            Collections.sort(a);
            for (int k = 0; k < 3; k++) {
                x[k][i] = a.get(k);
            }
            a = new ArrayList<>();
        }

        //for loop for testing purpose
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
               System.out.print(x[i][j] + ",");
            }
            System.out.println("\n");
        }
     }

15,5,22,
30,12,37,
55,87,40,

3 Comments

Could you post the output for the input you specified? I have a feeling this is not what OP wants (or, at lest, not what I think he wants).
@Turing85 what do you think?
After taking a closer look at the question... this seems to be a legit solution.
0

Why not create a temporary array to covert the columns to row and then sort the individual row and set the sorted row back to the original array.

Like:

public static double[][] sortColumns(double[][] array)
{
double[][] sorted = new double[3][3];
  for(int x = 0; x < 3; x++)
  {
     double[] column = new double[3]
     for(y =0; y < 3; y++){
         column[y] = array[y][x]; //convert column to array
     }
     Arrays.sort(column);
     for(y = 0; y < 3; y++){
         sorted[y][x] = column[y]; //convert array back to column
     }
  } //end loops
  return sorted;
} //end sortRows

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.