如何按字母顺序排列二维表?

问题描述

我有一个二维表,在他的第一行中有字母,在每个字母下方都有数字。我想按字母顺序排列第一行,但是对于每个更改的字母,我都希望下面的数字一起移动。

例如,我有此表:

C O R T I N A
1 1 0 0 3 4 0
4 3 3 1 2 1 3
2 1 2 1 2 2 0
0 2 3 0 2 0 4
2 3 3 4 0 4 3
2 

,结果必须是:

A C I N O R T
0 1 3 4 1 0 0
3 4 2 1 3 3 1
0 2 2 2 1 2 1
4 0 2 0 2 3 0
3 2 0 4 3 3 4
  2

我尝试这里是我的代码,但是它不起作用。在注释中是我认为自己完成的整个代码,但是没有用。然后,我尝试仅更改第一行,但仍然失败。有人可以帮我吗?

                for (int i=0; i<table2.length; i++) {
                for (int j=0; j<table2[0].length; j++) {
                    if (i==0 && ((int)table2[i][i]>(int)table2[i][j])) {
                        char d = table2[i][j];
                        table2[i][j]=table2[i][i];
                        table2[i][i]=(char)d;
//                          char [] help = new char [table2[j].length];
//                          for (int k=0; k<help.length; k++) {
//                              help[k]=table2[i+k][j];
//                              //System.out.print(help[k]);
//                          }
//
//                          for (int u=0; u<table2.length; u++) {
//                              for (int p=0; p<table2[0].length; p++) {
//                                  if (u==0 && (int)table2[u][u]>(int)table2[u][p]) {
//                                      table2[u][p] = table2[u][u];
//
//                                      for (int k=0; k<help.length; k++) {
//                                          table2[u][u] = help[k];
//                                      }
//                                  }
//                              }
//                          }

                    }



                    System.out.print((char)table2[i][j] + " ");
                    if (j==userKeyWord.length()-1)
                        System.out.println();
                }
            }

我忘了最后提到userKeyWord是用户给出的用于在下面打印数字的单词。 (ofc只是代码的一部分)

解决方法

我首先根据字母值对第一行的索引进行了排序。然后,我使用这些索引重新排列其他行以反映相同的位置。为了满足最后的孤立值,我添加了虚拟值-1来填充未使用的数组位置。这样可以使矩阵更加统一并且更易于使用。

int[][] mat = { { 'C','O','R','T','I','N','A' },{ 1,1,3,4,0 },{ 4,2,3 },{ 2,{ 0,4 },-1,-1 } };

获取第一行并基于该行对索引进行排序。

int[] first = mat[0];

int[] indices = IntStream.range(0,first.length).boxed()
        .sorted((a,b) -> Integer.compare(mat[0][a],mat[0][b]))
        .mapToInt(Integer::intValue).toArray();

现在使用这些索引来构建新矩阵

for (int row = 0; row < mat.length; row++) {
    int[] temp = mat[row];
    mat[row] = Arrays.stream(indices).map(i -> temp[i])
            .toArray();
}

然后打印


for (int i = 0; i < mat[0].length; i++) {
    System.out.printf(" %c ",mat[0][i]);
}
System.out.println();

for (int row = 1; row < mat.length; row++) {
    for (int col = 0; col < mat[row].length; col++) {
        int v = mat[row][col];
        System.out.printf("%2s ",v < 0 ? "" : v);
    }
    System.out.println();
}

打印

 A  C  I  N  O  R  T 
 0  1  3  4  1  0  0 
 3  4  2  1  3  3  1 
 0  2  2  2  1  2  1 
 4  0  2  0  2  3  0 
 3  2  0  4  3  3  4 
    2                

,

我不确定100%为什么您的代码无法正常工作,但是我的处理方式略有不同。这就是我所做的。

private static final char[][] unsortedArray = {{'C','A'},{1,0},{4,3},{2,{0,4},3}};

//Object to hold original index and key
private static class IndexTuple {
    protected char key;
    protected int unsortedIndex;

    public IndexTuple(char key,int unsortedIndex) {
        this.key = key;
        this.unsortedIndex = unsortedIndex;
    }
}

//Comparator to let collections sort for you
private static class TupleComparator implements Comparator<IndexTuple> {

    @Override
    public int compare(IndexTuple o1,IndexTuple o2) {
        return o1.key - o2.key;
    }
}

public static char[][] sortTable(char[][] unsortedTable) {
    //Create new array to copy result to
    char[][] sortedTable = new char[unsortedTable.length][unsortedTable[0].length];

    //Copy result to List of custom objects
    ArrayList<IndexTuple> tupleList = new ArrayList<>();
    for (int i = 0; i < unsortedTable[0].length; i++) {
        tupleList.add(new IndexTuple(unsortedTable[0][i],i));
    }

    //sort list
    tupleList.sort(new TupleComparator());

    //Using original unsorted index and new sorted index set all the values in correct order
    for (int i = 0; i < unsortedTable.length; i++) {
        for (int j = 0; j < tupleList.size(); j++) {
            sortedTable[i][j] = unsortedTable[i][tupleList.get(j).unsortedIndex];
        }

    }
    return sortedTable;
}