Praveen Mora
Praveen Mora

Reputation: 13

Swap rows in 2d array by row sum in Java

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;
        }
          }
        }
     }

Upvotes: 0

Views: 447

Answers (3)

Flown
Flown

Reputation: 11740

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

Upvotes: 2

Shiladittya Chakraborty
Shiladittya Chakraborty

Reputation: 4408

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,

Upvotes: 1

Balayesu Chilakalapudi
Balayesu Chilakalapudi

Reputation: 1406

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 

Upvotes: 0

Related Questions