Q:

Get index of sorted matrix

Suppose i enter a matrix and sort her rows, for example:
(3 5 1 4
4 3 7 2
1 0 9 7)
then sort the matrix :
(1 3 4 5
2 3 4 7
0 1 7 9)
i want show the index matrix after sorting the matrix
(2 0 3 1
3 1 0 2
1 0 3 2)
i use this code to sort the mattrix :
public static void sortRows(int[][] matrix){        
    for (int i = 0; i < matrix.length; i++) {            
        Arrays.sort(matrix[i]);
     }
}

The problem is: how can I find the maatrix of newIndex from the input matrix?
Thanks in advance

A:

You should do like this.
import java.util.Arrays;
import java.util.Comparator;

public class StackOverflow {

    public static void main(String[] args) {
        mat=new Element[3][4];
        int[][] gMat={{3 ,5 ,1 ,4},
                      {4 ,3 ,7 ,2},
                      {1 ,0 ,9 ,7}};
        for (int i=0;i<gMat.length;i++){
            for (int j=0;j<gMat[0].length;j++){
                mat[i][j]=new Element(j,gMat[i][j]);
            }
        }

        sort();
        for (int i=0;i<gMat.length;i++){
            for (int j=0;j<gMat[0].length;j++){
                System.out.print(mat[i][j].idx+" ");
            }
            System.out.println();
        }
    }

    static class Element{
        int idx;
        int val;
        Element(int idx,int val){
            this.idx=idx;
            this.val=val;
        }
    }

    static Element[][] mat;
    static void sort(){
        for (int i=0;i<mat.length;i++){
            Arrays.sort(mat[i], Comparator.comparingInt(o -> o.val));
        }
    }
}

