JavaFx - TableView:如何使用自定义 Comparator<T> 进行排序?

JavaFx - TableView: How to use custom Comparator<T> for sorting?

... 其中 TTableView 的通用类型。

到目前为止,我正在实施一个包含三列的 file-listview。每个类型 java.nio.file.Path。对于名称列,我写了一个Comparator<Path>,它用directories-first 和case-insensitiv 对文件进行排序。其他两个比较器按 last-modified-time 和 file-size 排序。为此,他们比较 long 个字段。

但是某列的comparatorProperty是基于Comparator<String>的。我认为,根据显示的文本进行排序...

所以我必须找到一种方法,将 sort-on-header-click 功能与 TableView?

的类型一起使用

您在创建 TableColumn 时使用了错误的类型参数。如果创建 TableColumn<Path, Path>,则可以为该列指定 Comparator<Path>。同样 TableColumn<Path, FileTime>TableColumn<Path, Long> 使用 Comparator<FileTime>Comparator<Long>.

来自文档:

Class TableColumnBase<S,T>
Type Parameters:
S - The type of the UI control (e.g. the type of the 'row').
T - The type of the content in all cells in this table column.