3

I have a 2d array called interval[g][2] where g is some number. Currently, I'm trying to sort the array first by increasing order in the first element, and if they are equal, sort by decreasing order in the second element.

I've attempted this in two ways:

1) Using Java 8's Comparator.comparing method:

Arrays.sort(interval, Comparator.comparing((int[] arr) -> arr[0]));

2) Using Arrays.sort:

Arrays.sort(interval, new Comparator<int[]>() {
    @Override
    public int compare(int[] s1, int[] s2) {
        if (s1[0] > s2[0])
            return 1;
        else if (s1[0] < s2[0])
            return -1;
        else {
            if(s1[1] < s2[1])
                return 1;
            else if (s1[1] > s2[1])
                return -1;
            else
                return 0;
        }
    }
});

The first method returns a partially sorted list.

[[0, 10], [10, 30], [30, 50]]
[[0, 10], [3, 19], [35, 45]]
[[10, 30], [27, 33], [30, 50]]
[[-10, 10], [0, 20], [35, 45]]
[[10, 30], [20, 40], [30, 50]]
[[0, 20], [8, 28], [37, 43]]
[[0, 20], [15, 35], [37, 43]]
[[0, 0], [8, 28], [10, 40]]

As you can see, it's sorting things in a set of three tuples.

The second method doesn't sort the array at all. Can I not sort using primitive data types? Can anyone advise?

4
  • 2
    What generated the output you show? Show the code. That looks like a 3d array. Commented May 10, 2016 at 23:17
  • 2
    That looks like 8 different examples of sorting a 2D array with g=3. They all look like they are sorted perfectly by the first value of the 3 pairs. Since no pair has the same value in position 1, how would you know if the secondary sort works? Commented May 10, 2016 at 23:31
  • @JimGarrison I was setting g according to an input provided--but you're right, I realized that I was never updating g for the following set of inputs. I have fixed my code and tried the answer below and it seems to work. Thank you! Commented May 11, 2016 at 4:45
  • @Andreas I never updated g value. Now it's fixed and with the answer below, the sorting works the way it should. Thank you! Commented May 11, 2016 at 4:46

2 Answers 2

9

I think you're looking for this:

Arrays.sort(interval, Comparator.comparingInt((int[] arr) -> arr[0]).thenComparing(Comparator.comparingInt((int[] arr) -> arr[1]).reversed()));

Or if you want to go with the custom Comparator:

 Arrays.sort(interval, new Comparator<int[]>() {
     @Override
     public int compare(int[] o1, int[] o2) {
         int result = Integer.compare(o1[0], o2[0]);
         if (result == 0) {
             result = Integer.compare(o2[1], o1[1]);
         }
         return result;
     }
 });
Sign up to request clarification or add additional context in comments.

1 Comment

Very nice. Op's #2 is working fine, but I like your shorter version (of course I do, I was about to post the same answer).
2
int[][] interval = new int[][] { {0, 10}, {10, 30}, {30, 50}, {0, 10}, {3, 19}, {35, 45}, {10, 30}, {27, 33}, {30, 50}, {-10, 10}, {0, 20}, {35, 45}, {10, 30}, {20, 40}, {30, 50}, {0, 20}, {8, 28}, {37, 43}, {0, 20}, {15, 35}, {37, 43}, {0, 0}, {8, 28}, {10, 40} };

Arrays.sort(interval, Comparator.<int[]>comparingInt(arr -> arr[0]).thenComparing(arr -> arr[1], Comparator.<Integer>naturalOrder().reversed()));

Stream.of(interval).forEachOrdered(ints -> System.out.println(Arrays.toString(ints)));

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.