7

how can i sort this array by date or name?

String[][] datetable= new String[21][2];

datetable[0][0] = "2011.01.01";
datetable[0][1] = "Name1";
datetable[1][0] = "2011.01.03";
datetable[1][1] = "Name2";
.
.
.
datetable[20][0] = "2011.02.16";
datetable[20][1] = "Name3";
1

2 Answers 2

8

I would do what the poster linked to, only I wouldn't use final so much.

Arrays.sort(datetable, new Comparator<String[]>() {
    @Override
    public int compare(String[] entry1, String[] entry2) {
        // Sort by date
        return entry1[0].compareTo(entry2[0]);
    }
});
Sign up to request clarification or add additional context in comments.

Comments

1

This may help: Sort a two dimensional array based on one column

4 Comments

Looks good. I would like to paste some code here how to do that? –
Thank you! Thats is the simple solution what i was looking for!
In certain languages there are special letters. In Hungary we have also e.g. Á, which follows letter A. In this sorting procedure words starting with these letters are at the end of the of the array. Is there a way to handle this?
It's a matter of how you override the "compare" function. In that code example, the "compare" function simply uses the default String.compareTo() to sort strings. The reason you found those words are put to the end is probably because their unicode is bigger than the unicode of any English letters. If you want to put Á right after A, you will need to implement a more sophisticated "compare" function.

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.