0

I implemented this thing but I am not sure this is correct

   Example : int [][]={{2,1,0},{2,8,9},{1,1,0}}

In the above sum of elements in row 1 (2+1+0=3) is lesser than sum of elements in row 2(2+8+9=19) and greater than row 3 sum(2).

The final array should be like {{2,8,9},{2,1,0},{1,1,0}}

    int arr[5][5];
    int rowSum[5];
    int sum = 0;
    for(int i=0;i<5;i++)
    {
        for(int j=0;j<5;j++)
        {
        sum = sum + arr[i][j];
        }
        rowSum[i] = sum;
        sum=0;
    }

    int swap;
    //now sorting
    for (int c = 0 ; c < ( n - 1 ); c++)
      {
        for (int d = 0 ; d < n - c - 1; d++)
        {
          if (rowSum[d] > rowSum[d+1]) /* For decreasing order use < */
          {
            swap       = rowSum[d];
            rowSum[d]   = rowSum[d+1];
            rowSum[d+1] = swap;

        //swapping original array
        for(int i=0;i<5;i++){
           swap = arr[d][i];
           arr[d][i] = arr[d+1][i];
               arr[d+1][i] = swap;
        }
          }
        }
     }
4
  • 1
    "I am not sure this is correct" - have you tested it? Looks good to me Commented Nov 9, 2015 at 5:46
  • 5
    Is there a good reason not to implement a Comparator<int[]> and use Arrays.sort? Commented Nov 9, 2015 at 5:48
  • stackoverflow.com/questions/18232788/… Check this link. Commented Nov 9, 2015 at 5:59
  • Hi there. Please do not add "urgent" to your posts, especially in the title - it is just noise to be edited out. Note that questions here are of equal importance. Thanks! Commented Nov 9, 2015 at 8:05

3 Answers 3

2

Using Java 8 features:

int[][] arr = { { 2, 1, 0 }, { 2, 8, 9 }, { 1, 1, 0 } };
Arrays.sort(arr, Comparator.comparingInt((int[] row) -> Arrays.stream(row).sum()).reversed());
System.out.println(Arrays.deepToString(arr));
Sign up to request clarification or add additional context in comments.

Comments

1
Integer[][] numbers = new Integer[][]{{2,1,0},{2,8,9},{1,1,0}};
System.out.println("Before:");
for(Integer[] row : numbers) {
  for(Integer num : row) {
    System.out.print(num+",");
  }
  System.out.println("");
}

Arrays.sort(numbers, new Comparator<Integer[]>(){
  @Override
  public int compare(Integer[] o1, Integer[] o2) {
    Integer sumArray_1 = 0;
    Integer sumArray_2 = 0;
    for(int i = 0; i < o1.length; ++i) {
      sumArray_1 += o1[i];
    }

    for(int i = 0; i < o2.length; ++i) {
      sumArray_2 += o2[i];
    }

    return sumArray_2.compareTo(sumArray_1); //Decending order 
  }
});
System.out.println("After:");
for(Integer[] row : numbers) {
  for(Integer num : row) {
    System.out.print(num+",");
  }
  System.out.println("");
}

Output:

Before:
2,1,0,
2,8,9,
1,1,0,
After:
2,8,9,
2,1,0,
1,1,0,

3 Comments

So, you think return o2[1].compareTo(o1[1]); is an appropriate way to compare integer arrays? Hint: no, it isn't. It also completely ignores the sum of each digit in that array.
Glad to see that you took the time to improve your answer. Please mind one little "issue": your for loop will have problems if each sub-array has a different length :P.
@Tom I know there is an issue and that can be solve easily.
0

Use the below Code Snippet,

        int[][] temp = {{2,1,0},{2,8,9},{1,1,0}};
        Arrays.sort(temp, new Comparator<int[]>() {
        @Override
        public int compare(int[] a, int[] b) {
            return Integer.compare(b[1], a[1]);
        }
        });
        System.out.println("After applying comparator");
        for(int[] arr:temp){
            for(int val:arr){
                System.out.print(val+" ");
            }
            System.out.println("");
        }

It will display the output like this,

After applying comparator
2 8 9 
2 1 0 
1 1 0 

4 Comments

So you think return Integer.compare(b[1], a[1]); is an appropriate way to compare integer arrays? Hint: no, it isn't. It also completely ignore the sum of each digit in that array.
@Tom: would you consider adding your own answer? :-).
@halfer Not necessary, because Flown already wrote a good one :) (also, the only really working one :D).
Ah right @Tom, you might want to ping the OP under the question, in that case!

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.