Eu fiz isto faz algum tempo, veja se isto lhe interessa.
import java.util.*;
/**
* Este Comparator destina-se a ordenar arrays Object[][] que são usados em JTables.
*
*/
class ComparadorJTable implements Comparator {
private boolean crescente = true;
private int coluna = 0;
private boolean numerico = false;
private boolean ignoraMaiusculas = true;
/**
* Use este construtor para definir um Comparator customizado.
* @param pCrescente Se a comparação deve ficar em ordem crescente ou decrescente.
* @param pColuna O número da coluna do Object[][] que vai ser usado como referência para ordenação.
* A primeira coluna é 0.
* @param pNumerico Se a comparação considera que os valores dessa coluna são numéricos.
* @param pIgnoraMaiusculas Se a comparação ignora maiúsculas.
*/
public ComparadorJTable (boolean pCrescente, int pColuna, boolean pNumerico, boolean pIgnoraMaiusculas) {
crescente = pCrescente;
coluna = pColuna;
numerico = pNumerico;
ignoraMaiusculas = pIgnoraMaiusculas;
}
/**
* @see java.util.Comparator
*/
public int compare (Object obj1, Object obj2) {
if (obj1 instanceof Object[] && obj2 instanceof Object[]) {
// Os objetos são comparáveis
int ret = 0;
int col = coluna;
int maxcol = ((Object[])obj1).length;
do {
ret = compareColuna ((Object[])obj1, (Object[])obj2, col);
col++;
} while (ret == 0 && col < maxcol);
return ret;
} else {
throw new ClassCastException("Use um Object [][]. O tipo de obj1 = "
+ (obj1 != null ? obj1.getClass().getName() : "null")
+ ", e o tipo de obj2 = "
+ (obj2 != null ? obj2.getClass().getName() : "null"));
}
}
/**
*/
private int compareColuna (Object[] obj1, Object[] obj2, int col) {
int ret;
String val1 = obj1[col].toString();
String val2 = obj2[col].toString();
if (numerico) {
double d1 = Double.NEGATIVE_INFINITY;
double d2 = Double.NEGATIVE_INFINITY;
try {
d1 = Double.parseDouble (val1.trim());
} catch (NumberFormatException ex) {
}
try {
d2 = Double.parseDouble (val2.trim());
} catch (NumberFormatException ex) {
}
ret = (d1 < d2) ? -1 : ((d1 > d2) ? +1 : 0);
} else if (ignoraMaiusculas) {
ret = val1.compareToIgnoreCase (val2);
} else {
ret = val1.compareTo (val2);
}
if (!crescente)
ret = -ret;
return ret;
}
}
class OrdenacaoNumerica {
private static void print(String msg, Object obj[][]) {
System.out.println ("-----");
System.out.println (msg);
System.out.println ("-----");
for (int i = 0; i < obj.length; ++i) {
Object[] row = obj[i];
for (int j = 0; j < row.length; ++j) {
System.out.print ("[" + obj[i][j] + "] ");
}
System.out.println ();
}
}
public static void main(String[] args) {
Object obj [][] = {
{ "100", "123", "345" },
{ "20", "abc", "123" },
{ "3", " tres palitos ", "22"},
{ "3", " tres palitos ", "12"},
{ "789", " anfoteros", "2345"},
};
print ("Original", obj);
Arrays.sort (obj, new ComparadorJTable (true, 0, true, true));
print ("Ordem crescente, primeira coluna", obj);
Arrays.sort (obj, new ComparadorJTable (false, 1, true, true));
print ("Ordem decrescente, segunda coluna", obj);
Arrays.sort (obj, new ComparadorJTable (true, 2, true, true));
print ("Ordem crescente, última coluna", obj);
}
}