Procedimiento para ordenar una matriz int bidimensional dependiendo de la columna
Le mostraré un ejemplo sobre el propósito de la pregunta. La matriz que tengo antes y cómo la queremos después de la clasificación:
Antes de :
Box Weight Priority
1 50 5
2 30 8
3 90 6
4 20 7
5 80 9
Después :
Box Weight Priority
3 90 6
5 80 9
1 50 5
2 30 8
4 20 7
Trabajamos en la matriz int:
data= new int[BoxNumber][3];
La clasificación se basa en el peso de la segunda columna. Estoy buscando un procedimiento que ordene la matriz de datos.
public void sortC(int[][] temp)
{
if (temp.length >= 2)
{
for (int i = 1; i <= temp.length - 1; i++)
{
int[] hold = temp[i];
int[] holdP = temp[i-1];
int j = i;
while (j > 0 && hold[1] < holdP[1]) // 1 represents the reference of sorting
{
hold = temp[j];
holdP = temp[j-1];
temp[j] = holdP;
temp[j-1] = hold;
j--;
}
}
}
}
sortC(data);
Intenté esto, pero desafortunadamente no da una clasificación correcta. No pude entender el problema. ¿Alguna ayuda por favor?