Sempre Invictus
Sempre Invictus

Reputation: 1

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. I want to know how I can sort a 2D array alphabetically in each column. 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

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

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

Thanks in advance!

Upvotes: 0

Views: 124

Answers (1)

JP Moresmau
JP Moresmau

Reputation: 7403

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

Upvotes: 1

Related Questions