0

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!

2
  • It is better to offer some code that you have tried. Commented May 28, 2015 at 15:04
  • create collections that correspond to each column, then sort them. For instance, your first List<String> would contain "The", "It", "A/an", etc. You'll have one collection for each "column". It should be straightforward how you populate each collection. Commented May 28, 2015 at 15:23

1 Answer 1

1

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();
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.