1

This is for row based sorting

Before sorting: array[2][n]

5 4 3 2 1.. 3 4 5 9 0..

After sorting:

1 2 3 4 5.. 0 9 5 4 3..

i.e Array should be sorted using 1st row alone in ascending order and corresponding column values should change accordingly as shown in above result.

Guide me how this can be generated using Arrays.sort(a, new Comparator() function?

int rowcount= jTable1.getRowCount();
int j;
int parr[][] = new int[10][10];
for(j=0; j<rowcount; j++)
{
    parr[0][j]= Integer.parseInt(jTable1.getValueAt(j, 2).toString()); 
    parr[1][j]= Integer.parseInt(jTable1.getValueAt(j, 1).toString());   
}

System.out.println("Before sorting");
for(j=0; j<rowcount; j++)
{
System.out.println(parr[0][j]);
}
 for(j=0; j<rowcount; j++)
{
System.out.println(parr[1][j]);
}

System.out.println("After sorting");

Arrays.sort(parr, new Comparator<int[]>() 
{
@Override
public int compare(int[] o1, int[] o2) 
{
    return Integer.compare(o2[1], o1[1]);
}
}
);

 for(j=0; j<rowcount; j++)
{
System.out.println(parr[0][j]);
}

2 Answers2

1

Firstly, reading columns in a 2-D Array as 1-D Array is not possible. It can only be done for rows.

As per your code, you are comparing o2[1] and o1[1] which means you are comparing 2nd element in both the rows which is not your requirement.

You better transpose the the matrix and apply the same logic by comparing o2[0] and o1[0] and finally transpose the matrix again to get the desired result..

SatishV
  • 393
  • 4
  • 22
1

This gets the job done using selection sort

This only handles two columns. If you want this to handle more columns, you have to used the same idea used to sort the second half.

public static void main(String[] args)
{
    // TODO code application logic here


    int[][] array2D =
    {
        {
            5, 3, 4, 2, 1
        },
        {
            10, 8, 6, 4, 1
        }
    };

    //One way of printing a 2D array
    System.out.print("Before:\n\t");
    for (int i = 0; i < array2D[0].length; i++)
    {
        System.out.print(array2D[0][i] + ", ");
    }
    System.out.println();
    System.out.print("\t");
    for (int i = 0; i < array2D[0].length; i++)
    {
        System.out.print(array2D[1][i] + ", ");
    }
    System.out.println();

    selectionSort(array2D);

    //Another way of printing a 2D array.
    System.out.print("After:\n\t");
    for (int[] item : array2D)
    {
        for (int entry : item)
        {
            System.out.print(entry + ", ");
        }
        System.out.println();
        System.out.print("\t");
    }
}

//Modified selection sort algrothim
public static void selectionSort(int[][] a1)
{
    for (int i = 0; i < a1[0].length; i++)//loop through first half of 2D array
    {
        int index = i;

        for (int j = i + 1; j < a1[0].length; j++)//loop through first half of 2D array
        {
            if (a1[0][j] < a1[0][index])
            {
                index = j;
            }
        }
        int smallerNumber = a1[0][index];//Sort the first half like you would a 1D array
        int smallerNumber2 = a1[1][index];//When a value will be moved in the first half sort, prepare the number in the same index to be swapped

        a1[0][index] = a1[0][i];//swap first half 
        a1[1][index] = a1[1][i];//swap corresponding number in second half

        a1[0][i] = smallerNumber;//complete first half swap
        a1[1][i] = smallerNumber2;//complete second half swap
    }
}
SedJ601
  • 12,173
  • 3
  • 41
  • 59