Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

On my quest learning Java I come across one doubt.

For sorting a unidimensional array we can use Arrays.sort() but if I want to sort a bidimensional array based on three columns? Is there any option to d that or do I have to code it for my self (something like three nested loops)?

Here is an example input:

13  2   28  36  
1   4   56  17  
4   2   5   40  
2   4   41  55  
9   5   48  12  
19  2   25  12  
20  5   13  8   
15  3   51  30  
12  5   39  59  
17  3   56  40  
3   1   56  46  
7   3   28  51  
8   5   14  58  
5   3   34  15  
14  4   53  2   
18  4   38  57  
6   2   16  25  
16  3   17  13  
10  5   41  33  
11  1   13  57  

Columns are int and this is stored in an array of ints.

I wanna sort by column 2, if equal numbers are found, then sort by column 3 and finally, if equals found, sort by column 3.

The output should be this:

11  1   13  57  
3   1   56  46  
4   2   5   40  
6   2   16  25  
19  2   25  12  
13  2   28  36  
16  3   17  13  
7   3   28  51  
5   3   34  15  
15  3   51  30  
17  3   56  40  
18  4   38  57  
2   4   41  55  
14  4   53  2   
1   4   56  17  
20  5   13  8   
8   5   14  58  
12  5   39  59  
10  5   41  33  
9   5   48  12  

Is there a simple way for doing this? Remember that I'm new at Java.

regards,

Favolas

share|improve this question
 
How is this data presented to you? –  DerMike Nov 10 '11 at 10:39
 
Sorry. My bad. Forget the last column (the doubles) Its an array of ints. Edited initial post –  Favolas Nov 10 '11 at 10:41
add comment

4 Answers

up vote 2 down vote accepted

Simple use Comparator and use Arrays.sort(arr, comparator);

share|improve this answer
 
Hi. Tried to understand but unfortunately wasn't able. Nevertheless, many thanks. –  Favolas Nov 10 '11 at 14:52
 
comparator is custom class that will enable you to compare the arrays elsment on your way so this can fix your issue –  Jigar Joshi Nov 10 '11 at 15:48
add comment

It depends on how you get the data.

In general, you could give your own Comparator to Collections.sort.

share|improve this answer
 
See also Ordering in guava –  RC. Nov 10 '11 at 10:43
add comment

Use the comparator below

class MyArrayComparator implements Comparator<Integer[]> {

  @Override
  public int compare(Integer[] o1, Integer[] o2) {
    if (o1[1] == o2[1]) {
      if (o1[2] == o2[2]) {
        return o1[3].compareTo(o2[3]);
      }
      else {
        return o1[2].compareTo(o2[2]);
      }
    }
    return o1[1].compareTo(o2[1]);
  }

}

Use the following sort method

Collections.sort(yourListOfArry, new MyArrayComparator());
share|improve this answer
 
+1: It could also be an int[] which might be more efficient. –  Peter Lawrey Nov 10 '11 at 11:15
 
@Kowser Thanks for your answer. Done what you said and created on main this and created a new class like this but its giving me error... –  Favolas Nov 10 '11 at 11:55
 
what error? IndexOutOfBound? are you passing a list of array? it might be helpful if you could post, how you are holding your array. –  Kowser Nov 10 '11 at 14:59
add comment

Implementing an Comparator on arrays of arrays seems kind of dangerous to me because you have to ensure manually, that all arrays in the second dimension are of the same length and the semantics of the components of the array must be the same at all times - for these kind of constraints objects should be used.

If the second dimension in the array has some meaning (i.e. it is always four becouse three are coordinates and the fourth is the value), you should consider not to model it as an array of arrays but an array of objects with four member variables (e.g. xCoord, yCoord, zCoord and value). You can then make this class implement the Comparable interface and implement the compareTo method. Then the Arrays.sort() method will sort according to the given compareTo method.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.