Bastava usare lo Swing Tutorial... comunque, una cosa del genere
codice:
import javax.swing.*;
import java.awt.*;
import javax.swing.table.*;
import java.util.*;
/**
*
* @author Andrea
*/
public class CustomJTableRowSorter extends JFrame {
private static class MyComparator implements Comparator {
public int compare(Object o1, Object o2) {
String s1 = o1.toString().trim().replaceAll("\\d", "");
String s2 = o2.toString().trim().replaceAll("\\d", "");
try {
Integer d1, d2;
try {
d1 = new Integer(o1.toString().trim().replaceAll("\\D", ""));
}
catch (Exception e) {
d1 = Integer.MIN_VALUE;
}
try {
d2 = new Integer(o2.toString().trim().replaceAll("\\D", ""));
}
catch (Exception e) {
d2 = Integer.MIN_VALUE;
}
return (s1.compareTo(s2) == 0 ?
(d1.compareTo(d2)) : s1.compareTo(s2)
);
}
catch (Exception e) {
e.printStackTrace();
return s1.compareTo(s2);
}
}
}
public CustomJTableRowSorter() {
super("Test Frame");
String[] columns = new String[] {"First", "Second"};
Object[][] data = {
{"p10", "HTML.IT"},
{"p2", "GOOGLE.COM"},
{"p", "UBUNTU.ORG"},
{"p20", "YAHOO.COM"},
{"p3", "MICROSOFT.COM"}
};
JTable table = new JTable(data, columns);
TableRowSorter sorter = new TableRowSorter(table.getModel());
sorter.setComparator(0, new MyComparator());
table.setRowSorter(sorter);
this.add(new JScrollPane(table));
this.setVisible(true);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main (String[] args) {
new CustomJTableRowSorter();
}
}
bada, non vuole essere esaustivo ed è stato scritto in circa 5 minuti, per cui il Comparator potrebbe non essere consistente... però mi sembra che ordini come vuoi tu.