How to sort JTable in shortest way?

As per How to Use Tables: Sorting and Filtering JTable table = new JTable(tableModel); TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(table.getModel()); table.setRowSorter(sorter); List<RowSorter.SortKey> sortKeys = new ArrayList<>(25); sortKeys.add(new RowSorter.SortKey(4, SortOrder.ASCENDING)); sortKeys.add(new RowSorter.SortKey(0, SortOrder.ASCENDING)); sorter.setSortKeys(sortKeys); Updated Are you sure it will sort? …Yes import java.awt.EventQueue; import java.util.ArrayList; import java.util.List; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.RowSorter; import … Read more

Sorting Table is wrong when the sort button be pressed more than once?

No where do you actually tell the TableModel that it’s contents have changed… Now, the thing that weirds me out, is you are keeping a Vector of the rows outside of the model… mS.merge_sort(0, rows.size()-1, n); temp.setSize(rows.size()); for(int i=0;i<index;i++){ temp.set(i, rows.get(n[i].ind())); } for(int i=0;i<index;i++){ rows.set(i, temp.get(i)); // This is doing nothing… } Once the data … Read more