DefaultTableModel model = (DefaultTableModel) table.getModel(); TableRowSortersorter = new TableRowSorter<>(model); sorter.setSortKeys(Collections.singletonList(new RowSorter.SortKey(0, SortOrder.ASCENDING))); table.setRowSorter(sorter);
DefaultTableModel model = (DefaultTableModel) table.getModel(); TableRowSorterIn this example, the DefaultTableModel is retrieved from the JTable object and used to create a TableRowSorter object. The setComparator method is used to set the comparison method for the second column of the table to natural order (numerical). The setSortKeys method is used to set the sort order to be ascending based on the second column of the table. Finally, the setRowSorter method is used to attach the sorter to the JTable object.sorter = new TableRowSorter<>(model); sorter.setComparator(1, Comparator.naturalOrder()); sorter.setSortKeys(Collections.singletonList(new RowSorter.SortKey(1, SortOrder.ASCENDING))); table.setRowSorter(sorter);