Se a coluna que você quer ordenar for uma String, ele ficará em ordem alfabética, causando o problema que você falou.
Uma solução é criar uma classe para representar os elementos da coluna a ordenar.
Essa classe precisa implementar a interface Comparable, fornecendo o método compareTo(). Nesse método você programa a forma como os elementos serão comparados para ordenar a tabela, o que dá uma grande flexibilidade.
Precisa também implementar o método toString() para informar como os elementos irão aparecer na tabela.
Abaixo um exemplo. A classe DoisValores implementa a interface Comparable. No método compareTo() informo como os elementos devem ser comparados.
Obs.: o método compareTo() retorna um inteiro negativo se o elemento da classe é menor que o argumento; 0 se são iguais e um valor positivo se é maior.
[code]import java.awt.;
import java.awt.event.;
import javax.swing.;
import javax.swing.table.;
import javax.swing.event.;
import java.util.;
import java.util.regex.*;
public class Estudos extends JFrame{
private TableRowSorter sorter;
public Estudos(){
super(“Tabela ordenada pela segunda coluna”);
// constrói a tabela
TableModel model = new MyTableModel();
final JTable tabela = new JTable(model);
tabela.setPreferredScrollableViewportSize(new Dimension(350, 100));
sorter = new TableRowSorter<TableModel>(model);
ArrayList sortKeys = new ArrayList();
sortKeys.add(new RowSorter.SortKey(1, SortOrder.ASCENDING));
sorter.setSortKeys(sortKeys);
tabela.setRowSorter(sorter);
Container c = getContentPane();
c.setLayout(new FlowLayout());
JScrollPane scrollPane = new JScrollPane(tabela);
c.add(scrollPane);
setSize(400, 300);
setVisible(true);
}
public static void main(String args[]){
Estudos app = new Estudos();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MyTableModel implements TableModel {
// colunas da tabela
private String[] columns = {“Cidade”, “Valores”, “Habitantes”};
// conteúdo da tabela
private Object[][] rows = {
{"Goiânia", new DoisValores(10,12), "43.023.432"},
{"São Paulo", new DoisValores(5,13), "5.343.234"},
{"Rio de Janeiro", new DoisValores(9,15), "6.434.212"},
{"Jussara", new DoisValores(8,9), "87.454"},
{"Barra do Garças", new DoisValores(20,6), "64.344"}
};
public String getColumnName(int col) {
return columns[col].toString();
}
public int getRowCount() { return rows.length; }
public int getColumnCount() { return columns.length; }
public Object getValueAt(int row, int col) {
return rows[row][col];
}
public boolean isCellEditable(int row, int col)
{ return true; }
public void setValueAt(Object value, int row, int col) {
rows[row][col] = value;
//fireTableCellUpdated(row, col);
}
public void addTableModelListener(TableModelListener l){
}
public void removeTableModelListener(TableModelListener l){
}
public Class getColumnClass(int column) {
Class returnValue;
if ((column >= 0) && (column < getColumnCount())) {
returnValue = getValueAt(0, column).getClass();
} else {
returnValue = Object.class;
}
return returnValue;
}
}
class DoisValores implements Comparable{
int valor1,valor2;
public DoisValores(int valor1, int valor2){
this.valor1 = valor1;
this.valor2 = valor2;
}
public String toString(){
return valor1+"/"+valor2;
}
public int getValor1(){
return valor1;
}
public int getValor2(){
return valor2;
}
public int compareTo(DoisValores d){
return this.getValor1() - d.getValor1();
}
}
[/code]