ObservableArrayList.sorted适用于原始数据类型但是如果引用数据类型不能正常工作
ObservableList<NewClass> observableArrayList = FXCollections.observableArrayList();
observableArrayList.add(new NewClass(1, "name 1"));
observableArrayList.add(new NewClass(2, "name 2"));
observableArrayList.add(new NewClass(4, "name 4"));
observableArrayList.add(new NewClass(5, "name 5"));
observableArrayList.add(new NewClass(3, "name 3"));
observableArrayList.sorted(Comparator.reverseOrder());
class NewClass
private int stt
private int name
错误消息
incompatible types: inference variable T has incompatible bounds
equality constraints: sf.enforcement.NewClass
upper bounds: java.lang.Comparable<? super T>
我想按照Newclass的顺序排序,请帮帮我
答案 0 :(得分:3)
Comparator.reverseOrder
仅在有逆转订单时才有效。仅当NewClass
实现Comparable
时才会出现这种情况。否则,您需要使用自定义比较器:
observableArrayList.sort((a, b) -> Integer.compare(b.getStt(), a.getStt()));
或
Comparator<NewClass> comparator = Comparator.comparingInt(NewClass::getStt);
observableArrayList.sort(comparator.reversed());