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
{
for (int j=0;j
{
ret[i][j] = matrix[order[i]][j];
}
}
return ret;
}
private static void fill(double[][] matrix)
{
for (int i=0;i
{
for (int j=0;j
{
matrix[i][j] = Math.random()*10;
}
}
}
private static int[] getOrder(double[][] matrix)
{
double[] sums = new double[matrix.length];
for (int i=0;i
{
for (int j=0;j
{
sums[i]+=matrix[i][j];
}
}
int[] order = new int[sums.length];
for (int i=0;i
{
order[i] = i;
}
for (int i=0;i
{
for (int j=i+1;j
{
if(sums[i]
{
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
{
double sum = 0.0;
for (int j=0;j
{
System.out.print(matrix[i][j]+" ");
sum+=matrix[i][j];
}
System.out.println(" sum: "+sum);
}
System.out.println();
}
}