I am trying to sort a 2D array
of integers in Java in increasing order according to the values of every column.
Let me explain my objective with the following example:
This is my array:
int[][] array = new int[][]{
{7, 3, 9},
{9, 1, 3},
{5, 8, 8}};
Here is the expected array:
int[][] newArray = new int[][]{
{5, 1, 3},
{7, 3, 8},
{9, 8, 9}};
As can see in the example, every values on newArray
are the same as array
but now ordered in each column in increasing order.
Almost all the questions in the forum are focused on how to sort a 2D array according to the values of a row or column, but I need this for every column.
You could do it like this.
sortByColumn
method calls this lambda for each number of columns.Prints