2

With my following code I try to sort a two dimensional array

 int[][] d2 = {
           {4,5,1},
           {4,1,1},
           {1,7,1},
           {3,3,2},
           {1}
          };

        java.util.Arrays.sort(d2, new java.util.Comparator<int[]>() {
            public int compare(int[] a, int[] b) {
                return a[0] - b[0];
            }
        });

I display the array after sorting

for (int r=0; r<d2.length; r++) {
            for (int c=0; c<d2[r].length; c++) {
                System.out.print(" " + d2[r][c]);
            }
            System.out.println("");
}

The Result I get is like this

 1 7 1
 1
 3 3 2
 4 5 1
 4 1 1

I want the result two come like this

 1
 1 7 1
 3 3 2
 4 5 1
 4 1 1

What is required to be done to get the array sorted like the above?

I tried replacing the {1} with {1,0,0} but it still gives the same result even for {1,0,1}.

5
  • 1
    You need to add more condition in the comparator (return the difference in array length if first element is equal). Commented Oct 9, 2012 at 11:24
  • do you want the comparison only on 1st elements? Commented Oct 9, 2012 at 11:26
  • I agree with @nhahtdh, you need to modify your compare method to handle the length of the arrays too. Commented Oct 9, 2012 at 11:27
  • @Sangeet Menon: Could you please state how you wan't arrays with the same first element should be sorted? Also it would be helpful to know how you would like to treat arrays of different lengths: By length only or by their elements also? Commented Oct 9, 2012 at 11:39
  • @SangeetMenon.. Do remember to accept one of the answers, that satisfies your need.. Commented Oct 9, 2012 at 12:01

2 Answers 2

4

Compare the length of passed array in the compare method.. If length of a[] is less than b[], return -1: -

    java.util.Arrays.sort(d2, new java.util.Comparator<int[]>() {
                public int compare(int[] a, int[] b) {
                    if (a.length != b.length) {
                        return a.length < b.length ? -1 : 1;
                    }
                    return a[0] - b[0];
                }
            });

If you want to sort, by checking each element, which I think you should do this way.. Change your compare method to: -

public int compare(int[] a, int[] b) {

    if (a.length == b.length) {
        for (int i = 0; i < a.length; i++) {
            if (a[i] == b[i]) {

            } else {
                return a[i] - b[i];
            }
        }
        return 0;

    } else {
        return a.length < b.length ? -1 : 1;
    }                       
}
Sign up to request clarification or add additional context in comments.

6 Comments

I don't think that's what the OP wants - you need to compare a[i]-b[i] until one of the array's length is reached and compare the other items with 0 (I suppose).
@assylias.. May be, but when I saw the expected output of OP, I though he wants this only.. You can see there.. He is not comparing all the elements..
@assylias How do I compare all the elements?
@assylias.. Edited the code to consider both the cases.. OP can choose whatever he wants now.. :)
@RohitJain OP is just trying to sort on first elements, i guess. my answer explains the reasons of results that OP gets.
|
0

In your Comperator you only use the first field to compare 2 arrays. You have to include all the fields to compare them or at least you have to check the length of the arrays to get your desired result.

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.