I'm trying to set up a method that will sort a two dimensional array of doubles by column. Based on the provided specifications, this method is also not supposed to take ragged arrays with rows of unequal lengths. I'm testing this using the double[][] mdarray = {{3.0, 4.0, 1.0, 8.0}, {13.0, 2.0, 12.0, 9.0}
Using a print method, this should be displayed as
3.0, 2.0, 1.0, 8.0,
13.0, 4.0, 12.0, 9.0,
When using a separate print method to output the result, the array appears to be unchanged. I think my problem is figuring out how to check every row for a given column before moving onto the next, but I'm not sure how to do that easily without the Arrays.sort() method, which is not allowed for this exercise.
public void sort(boolean byColumn)
{
if(isRagged() == true)
{
System.out.println("Ragged arrays cannot be sorted by column.");
}
else
{
for(int i = 0; i < mdarray.length; i++)
{
for(int j = i + 1; j < mdarray.length - 1; j ++)
{
for(int k = 0; k < mdarray[i].length; k++)
{
if(mdarray[i][k] > mdarray[j][k])
{
double temp = mdarray[i][k];
mdarray[i][k] = mdarray[j][k];
mdarray[i][k] = temp;
}
}
}
}
}
}
mdarray[i][k] = temp;should bemdarray[i][j] = temp;mdarray[j][k] = temp