Java 如何对二维字符串数组进行完全排序

标签 java arrays sorting

大家好,我搜索了有关 java 中的二维数组排序的网站,它们都只涉及一列。我想知道如何在每一列中按字母顺序对二维数组进行排序。我尝试过使用比较器,但它只对一列进行排序。

我必须按每一列按字母顺序或反向字母顺序(取决于用户的选择)输出我的词库并输出一个表格。

String word[][] ={ 
            {"The", "ball", "the", "book"},
            {"It", "dog", "a/an", "efficiently"},
            {"A/An", "has", "some", "dog"},
            {"Laura", "ant", "rolled", "cat"},
            {"William", "cat", "ran", "apple"},
            {"Alex", "ate", "big", "pear"},
            {"Chris", "smelled", "small", "slowly"},
            {"Daniel", "planted", "jumped", "truck"},
            {"Joshua", "washed", "rotten", "awkward"},
            {"Rachel", "bear", "juicy", "shirt"},
            {"Jimmy", "boiled", "roared", "plant"},
            {"Emily", "liked", "vibrant", "away"},
            {"Erin", "touched", "swam", "chair"},
            {"Michael", "hippo", "long", "bicep"},
            {"Steven", "grabbed", "short", "phone"},
            {"Andrew", "kept", "massive", "quickly"},
    };

因此示例输出将是:

A/An "\t" ant "\t" a/an "\t" apple

Andrew "\t" ate "\t" juicy "\t" away

Alex "\t" ball "\t" jumped "\t" book

提前致谢!

最佳答案

您需要按列对数据重新排序,并对列进行排序。例如:

    String word[][] ={ 
            {"The", "ball", "the", "book"},
            {"It", "dog", "a/an", "efficiently"},
            {"A/An", "has", "some", "dog"},
            {"Laura", "ant", "rolled", "cat"},
            {"William", "cat", "ran", "apple"},
            {"Alex", "ate", "big", "pear"},
            {"Chris", "smelled", "small", "slowly"},
            {"Daniel", "planted", "jumped", "truck"},
            {"Joshua", "washed", "rotten", "awkward"},
            {"Rachel", "bear", "juicy", "shirt"},
            {"Jimmy", "boiled", "roared", "plant"},
            {"Emily", "liked", "vibrant", "away"},
            {"Erin", "touched", "swam", "chair"},
            {"Michael", "hippo", "long", "bicep"},
            {"Steven", "grabbed", "short", "phone"},
            {"Andrew", "kept", "massive", "quickly"},
    };
    // generate the list of columns
    List<List<String>> cols=new ArrayList<>();
    for (String[] row:word){
        for (int a=0;a<row.length;a++){
            // create columns when needed
            while(cols.size()<a+1){
                cols.add(new ArrayList<String>());
            }
            List<String> col=cols.get(a);
            col.add(row[a]);
        }
    }
    // rewrite sorted words in word array
    int colIdx=0;
    for (List<String> col:cols){
        // sort column
        Collections.sort(col);
        for (int a=0;a<col.size();a++){
            word[a][colIdx]=col.get(a);
        }
        colIdx++;
    }
    // print
    for (String[] row:word){
        String sep="";
        for (String w:row){
            System.out.print(sep);
            sep="\t";
            System.out.print(w);
        }
        System.out.println();
    }

关于Java 如何对二维字符串数组进行完全排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30510124/

相关文章:

javascript - 按最高赞成票与反对票比率对投票数组进行排序,反之亦然

java - 在抽屉导航中禁用暗淡化

java - 套接字数组

java - 何时收集垃圾

c++ - 类的静态计数器值无法初始化数组

arrays - 在 CL 的 dotimes 循环中使用 aref?

Java 按字母顺序将两个文本文件排序为一个文本文件

python - 如何根据字符串列表中的数字对字符串列表进行排序?

java - XPage Java 类实例化为数据源

Java Apache POI 打开文件?