简体   繁体   English

Java如何完全对2D字符串数组进行排序

[英]Java How do I sort a 2D string array entirely

Hi guys I've searched the site about 2D array sorting in java and they all involved only one column. 嗨,大家好,我搜索了有关Java中2D数组排序的网站,它们都只涉及一列。 I want to know how I can sort a 2D array alphabetically in each column. 我想知道如何在每一列中按字母顺序对2D数组排序。 I've tried using comparators but it only sorts one column. 我试过使用比较器,但它只对一列进行排序。

I have to output my word bank in alphabetical or reverse alphabetical (depending on user's choice) by each column and output a table. 我必须按每一列以字母或反向字母(取决于用户的选择)输出我的词库,并输出一个表格。

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"},
    };

So the example output will be: 因此,示例输出为:

A/An "\\t" ant "\\t" a/an "\\t" apple A /一个“ \\ t”蚂蚁“ \\ t”一个/一个“ \\ t”苹果

Andrew "\\t" ate "\\t" juicy "\\t" away 安德鲁“ \\ t”吃了“ \\ t”多汁的“ \\ t”

Alex "\\t" ball "\\t" jumped "\\t" book 亚历克斯“ \\ t”球“ \\ t”跳了“ \\ t”书

Thanks in advance! 提前致谢!

You need to reorder your data by columns, and sort the columns. 您需要按列对数据重新排序,然后对列进行排序。 For example: 例如:

    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();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM