I have this 2d array that sorts the rows but i can't sort the column.. i can't figure it out for the life of me. PLEASE HELP

Please enter size of row between 1 - 20: 5
Please enter size of column between 1 - 20: 5 
Your matrix is 5 X 5 array: 
The 2D array before the sorting: 
105  710  517  769  76  
660  817  926  5  702  
698  574  420  41  911  
767  753  936  191  299  
512  256  804  46  946  

The 2D array after sorting the row: 
769  710  517  105  76  
926  817  702  660  5  
911  698  574  420  41  
936  767  753  299  191  
946  804  512  256  46  

Code:

import java.util.Scanner; // Scanner is in java.util

public class sortmatrix {

    public static void main(String args[]) {

    //Create a Scanner
    Scanner input = new Scanner(System.in);


    //Received one positive integer m from the user
    System.out.print("Please enter size of row between 1 - 20: ");
    int integerOne = input.nextInt();

    while ((integerOne > 20) || (integerOne < 1)){
        System.out.print("Please enter size of row between 1 - 20: ");
        integerOne = input.nextInt();
        }

    //Received one positive integer n from the user
    System.out.print("Please enter size of column between 1 - 20: ");
    int integerTwo = input.nextInt();

    while ((integerTwo > 20) || (integerTwo < 1)){
        System.out.print("Please enter size of column between 1 - 20: ");
        integerTwo = input.nextInt();
        }


    //Create a m x n array of integers for array2
    int [][] array2 = new int [integerOne][integerTwo];


    //Display the 2D array elements
    System.out.println("Your matrix is " + integerOne + " X " + integerTwo + " array: " );
    System.out.println("The 2D array before the sorting: ");
    printMatrix(array2);


    //Sorting each row in descending order
    System.out.println("The 2D array after sorting the row: ");

    sort2D(array2);

}



//-------------------------------------------------------------------------------------------------
/*The method for printing the value */


    public static void printMatrix (int[][] array2){

    /*Generate random integer numbers for this array elements.
    Each element number should range between 1 and 999 inclusive. */

    for (int row = 0; row < array2.length; row++){
        for (int column = 0; column < array2[0].length; column++){
            array2[row][column] = (int)(Math.random() * 999) + 1;

            //Display the 2D array elements (each row per line).
            System.out.print(array2[row][column] + "  ");
        }
        System.out.println();
    }

    }
//----------------------------------------------------------------------------------
/*binary search */


public static int binarySearch(long[] a, long key) {
   int bot = 0;
   int top = a.length - 1;
   while (bot <= top) {
      int mid = bot + (top - bot) / 2;
      if      (key < a[mid]) top = mid - 1;
      else if (key > a[mid]) bot = mid + 1;
      else return mid;
   }
   return -1;
} 

//-------------------------------------------------------------------------------------
/* The method for sorting the array elements */

public static void sort2D (int [][] array2){
        //Sorting the row of 2D array in descending order

        for (int row = 0; row < array2.length; row++){
            for(int column = 0; column < array2[0].length; column++) {
                 for (int columnTwo = column + 1; columnTwo< array2[0].length; columnTwo++){
                   if(array2[row][columnTwo]>array2[row][column])
                    {
                    int temp=array2[row][column];
                    array2[row][column] = array2[row][columnTwo];
                    array2[row][columnTwo]=temp;
                    }
                }
            }
         }
    // print sorted array
    for (int row = 0; row < array2.length; row++){
        for (int column = 0; column < array2[0].length; column++){
            System.out.print(array2[row][column] + "  ");
        }
        System.out.println();
    }



}
}

Recommended Answers

All 4 Replies

I have this 2d array that sorts the rows but i can't sort the column.. i can't figure it out for the life of me. PLEASE HELP
}

Hi :)

Since you have done all the hard work already and I have used your code as a base it is easier to show you by giving you the code than by explaining with words. The only thing I really have done is turned it around.

/* The method for sorting the array elements in the columns */
public static void sortC2D (int [][] array2)
{
   //Sorting the column of 2D array in descending order
    for(int column = 0; column < array2[0].length; column++)
    {
        for (int row = 0; row < array2.length; row++)
       {
          for (int rowTwo = row + 1; rowTwo < array2.length; rowTwo++)
          {
             if(array2[rowTwo][column]>array2[row][column])
            {
                int temp=array2[row][column];
               array2[row][column] = array2[rowTwo][column];
               array2[rowTwo][column]=temp;
           }
       }
   }
}
// print sorted array
for (int row = 0; row < array2.length; row++){
for (int column = 0; column < array2[0].length; column++){
System.out.print(array2[row][column] + " ");
}
System.out.println();
}
}
//Sorting each column  in descending order 
System.out.println("The 2D array after sorting the column: ");
sortC2D(array2);

I have only tested it a couple of times with small matrix's so please test it several times so it works properly.
Hope this helped you.
Good luck :)

Hi :)

Since you have done all the hard work already and I have used your code as a base it is easier to show you by giving you the code than by explaining with words. The only thing I really have done is turned it around.

/* The method for sorting the array elements in the columns */
public static void sortC2D (int [][] array2)
{
   //Sorting the column of 2D array in descending order
    for(int column = 0; column < array2[0].length; column++)
    {
        for (int row = 0; row < array2.length; row++)
       {
          for (int rowTwo = row + 1; rowTwo < array2.length; rowTwo++)
          {
             if(array2[rowTwo][column]>array2[row][column])
            {
                int temp=array2[row][column];
               array2[row][column] = array2[rowTwo][column];
               array2[rowTwo][column]=temp;
           }
       }
   }
}
// print sorted array
for (int row = 0; row < array2.length; row++){
for (int column = 0; column < array2[0].length; column++){
System.out.print(array2[row][column] + " ");
}
System.out.println();
}
}
//Sorting each column  in descending order 
System.out.println("The 2D array after sorting the column: ");
sortC2D(array2);

I have only tested it a couple of times with small matrix's so please test it several times so it works properly.
Hope this helped you.
Good luck :)

Thanks for the reply :) ... i tried the code out... and it sorts the columns out seperately; I was trying to get the rows and column to sort out in the same matrix.

i tried combining your code to my code and nogo. how would i get the column to sort out after the rows are sorted. Thanks again

still need help!!!

Thanks for the reply :) ... i tried the code out... and it sorts the columns out seperately; I was trying to get the rows and column to sort out in the same matrix.

i tried combining your code to my code and nogo. how would i get the column to sort out after the rows are sorted. Thanks again

Hi :)
I am sorry but I do not really understand what you mean.
The columns are sorted out after the rows are sorted.

Take a look at this output:
Please enter size of row between 1 - 20: 4
Please enter size of column between 1 - 20: 4
Your matrix is 4 X 4 array:

The 2D array before the sorting:
61 963 708 640
453 875 472 835
140 847 62 399
120 550 878 701

The 2D array after sorting the row:
963 708 640 61
875 835 472 453
847 399 140 62
878 701 550 120

//At this stage you see that every row is sorted in descending order

The 2D array after sorting the column:
963 835 640 453
878 708 550 120
875 701 472 62
847 399 140 61

//At this stage you see that every column is sorted in descending order and so is every row.

It is the same same matrix you sort in both cases.

If you should chose to sort the columns before the rows the final output would be different and look like this:
963 878 835 453
875 708 701 140
847 640 472 120
550 399 62 61

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.