Add custom sorting algorithm to TableColumn

Marco :

here's my problem:

Set a custom sorting algorithm for the default sorting of a TableColumn.

Background: As an example column, I want to use a column that represents IPv4 addresses as a String. So the default sorting is lexicographic (1.1.1.1, 1.1.1.10, 1.1.1.2). But I would like to have a numerical sorting (1.1.1.1, 1.1.1.2, 1.1.1.10).

The sorting algorithm is not the problem. I can trigger this sorting with a Button and the display in the TableView is correct. But what I want to do is to click on the column header and thereby call up my sorting algorithm.

General conditions: The data in the table is in an ObservableList, which is wrapped in a SortedList.

My question at this point is: How can I realize this?

M. S. :

You need to bind your comparators. When you click on a column's header, the column's comparator will be used, if you click again, the reversed order of that comparator will be used. If you're using a SortedList, and you have a different comparator for each column, bind the comparator property of the list to the table's comparator property. This way, when you click on the header, your list will also be notified and your data will be updated:

columnA.setComparator(comparator1);
columnB.setComparator(comparator2);
columnC.setComparator(comparator3);

ObservableList<MyData> items = FXCollections.observableArrayList();
SortedList<MyData> sortedList = new SortedList<>(items);
sortedList.comparatorProperty().bind(tableView.comparatorProperty());

tableView.setItems(sortedList);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=171163&siteId=1