public class MyClass {
public static void main(String args[]) {
int dimention1 = 10;
int dimention2 = 5;//40
double[][] matrix = new double[dimention1][dimention2];
fill(matrix);
print(matrix);
matrix = sort(matrix, getOrder(matrix));
}
private static double[][] sort(double[][]matrix, int[] order)
{
double[][] ret = new double[matrix.length][matrix[0].length];
for (int i=0;i<matrix.length;i++ )
for (int j=0;j<matrix[i].length;j++ )
ret[i][j] = matrix[order[i]][j];
return ret;
private static void fill(double[][] matrix)
matrix[i][j] = Math.random()*10;
private static int[] getOrder(double[][] matrix)
double[] sums = new double[matrix.length];
sums[i]+=matrix[i][j];
int[] order = new int[sums.length];
for (int i=0;i<sums.length;i++ )
order[i] = i;
for (int j=i+1;j<sums.length;j++ )
if(sums[i]<sums[j])
double tmp1 = sums[i];
int tmp2 = order[i];
sums[i] = sums[j];
order[i] = order[j];
sums[j] = tmp1;
order[j] = tmp2;
return order;
private static void print(double[][] matrix )
double sum = 0.0;
System.out.print(matrix[i][j]+" ");
sum+=matrix[i][j];
System.out.println(" sum: "+sum);
System.out.println();
public class MyClass {
public static void main(String args[]) {
int dimention1 = 10;
int dimention2 = 5;//40
double[][] matrix = new double[dimention1][dimention2];
fill(matrix);
print(matrix);
matrix = sort(matrix, getOrder(matrix));
print(matrix);
}
private static double[][] sort(double[][]matrix, int[] order)
{
double[][] ret = new double[matrix.length][matrix[0].length];
for (int i=0;i<matrix.length;i++ )
{
for (int j=0;j<matrix[i].length;j++ )
{
ret[i][j] = matrix[order[i]][j];
}
}
return ret;
}
private static void fill(double[][] matrix)
{
for (int i=0;i<matrix.length;i++ )
{
for (int j=0;j<matrix[i].length;j++ )
{
matrix[i][j] = Math.random()*10;
}
}
}
private static int[] getOrder(double[][] matrix)
{
double[] sums = new double[matrix.length];
for (int i=0;i<matrix.length;i++ )
{
for (int j=0;j<matrix[i].length;j++ )
{
sums[i]+=matrix[i][j];
}
}
int[] order = new int[sums.length];
for (int i=0;i<sums.length;i++ )
{
order[i] = i;
}
for (int i=0;i<sums.length;i++ )
{
for (int j=i+1;j<sums.length;j++ )
{
if(sums[i]<sums[j])
{
double tmp1 = sums[i];
int tmp2 = order[i];
sums[i] = sums[j];
order[i] = order[j];
sums[j] = tmp1;
order[j] = tmp2;
}
}
}
return order;
}
private static void print(double[][] matrix )
{
for (int i=0;i<matrix.length;i++ )
{
double sum = 0.0;
for (int j=0;j<matrix[i].length;j++ )
{
System.out.print(matrix[i][j]+" ");
sum+=matrix[i][j];
}
System.out.println(" sum: "+sum);
}
System.out.println();
}
}