简体   繁体   中英

How to use Arrays.sort to sort a 3 by 3 array by columns?

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.

你如何先转置它,然后对组件数组进行排序,然后再转置回来?

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 .

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

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();
}

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,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM