1

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

2
  • How is this data presented to you? Commented Nov 10, 2011 at 10:39
  • Sorry. My bad. Forget the last column (the doubles) Its an array of ints. Edited initial post Commented Nov 10, 2011 at 10:41

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Hi. Tried to understand but unfortunately wasn't able. Nevertheless, many thanks.
comparator is custom class that will enable you to compare the arrays elsment on your way so this can fix your issue
1

It depends on how you get the data.

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

Comments

1

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

3 Comments

+1: It could also be an int[] which might be more efficient.
@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...
what error? IndexOutOfBound? are you passing a list of array? it might be helpful if you could post, how you are holding your array.
0

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.

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.