2

I need to sort such an array by descending the elements of the first column. In the case of equality of elements in the first column, the elements of the second column must be sorted in ascending order. But this what I need else is to check and put empty row to the end of matrix, and row with only one element put prior to the same which has more elements. For example in this array {3} - it is the first row, {} - the last one.

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

 Arrays.sort(arr, new Comparator<int[]>() {
     @Override
     public int compare(int[] o1, int[] o2) {
        if(o1[0] == o2[0])
            return o1[1] - o2[1];
        return o2[0] - o1[0];
    }
});

for (int[] ints : arr) {
    for (int anInt : ints) {
        System.out.print(anInt + " ");
    }
    System.out.println();
}
6
  • Check this it might help stackoverflow.com/questions/15452429/java-arrays-sort-2d-array Commented Apr 7, 2020 at 19:43
  • How do you want to compare irregular array items with each other? Is {} considered as higher or lower than {3}? And how about {1, 2, 3} and {1, 4}? Commented Apr 7, 2020 at 19:44
  • so, every empty row goes to the very end of array, and if we have {3} and {3, 0, 2} - {3} goes prior to {3, 0, 2} Commented Apr 7, 2020 at 19:48
  • How about {1, 2, 3} and {1, 2}? Commented Apr 7, 2020 at 19:49
  • 1
    @ВладГригоренко: I have edited my answer to fit the last criterion. Commented Apr 7, 2020 at 20:02

1 Answer 1

4

The following Comparator<int[]> enables sorting by:

  1. Empty arrays as last

  2. Bigger number at the same index in the ascending order

  3. In case the smaller array first into larger (in terms of length) starting at index 0, which one comes first is considered smaller compared to the latter one.

Here is an algorithm can be easily modified according to your needs:

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

Arrays.sort(arr, (o1, o2) -> {
        if (o1.length == 0) { return 1; }         // empty last
        if (o2.length == 0) { return -1; }        // empty last
        int min = Math.min(o1.length, o2.length); // upper bound to avoid ArrayIndexOutOfBoundsException
        for (int i = 0; i < min ; i++) {
            if (o1[i] != o2[i]) {                 // compare values on indices
                return o1[i] - o2[i];             // return if different
            } 
        }
        return 1;                                 // it goes first so it lefts first
    }
);

System.out.println(Arrays.deepToString(arr)); 

The output will be:

[[1, 2, 3], [1, 4], [3], [3, 2, 2], [3, 2], [3, 3, 5], []]

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

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.