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";
dave.c
  • 10,910
  • 5
  • 39
  • 62
erdomester
  • 11,789
  • 32
  • 132
  • 234
  • possible duplicate of [Sort a two dimensional array based on one column](http://stackoverflow.com/questions/4907683/sort-a-two-dimensional-array-based-on-one-column) – andri Feb 12 '11 at 21:31

2 Answers2

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]);
    }
});
Edawg
  • 258
  • 1
  • 4
1

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

Community
  • 1
  • 1
evergreen
  • 736
  • 1
  • 9
  • 17
  • Looks good. I would like to paste some code here how to do that? – – erdomester Feb 12 '11 at 23:45
  • Thank you! Thats is the simple solution what i was looking for! – erdomester Feb 13 '11 at 08:54
  • 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? – erdomester Feb 13 '11 at 15:06
  • 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. – evergreen Feb 15 '11 at 06:53