[英]Sort List based on multiple properties Collection.sort() Compile error
I need to sort a list using multiple properties, I tried this code but I am getting a compilation error 我需要使用多个属性对列表进行排序,我尝试了此代码,但出现编译错误
package com.demo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import lombok.Data;
import lombok.extern.log4j.Log4j;
@Log4j
public class TestSort {
public static void main(String[] args) {
List<Kot> kots = new ArrayList<Kot>(){{
add(new Kot("aa",1));
add(new Kot("vv",1));
add(new Kot("zz",2));
add(new Kot("bb",3));
add(new Kot("cc",1));
}};
log.info(kots);
Collections.sort(kots);
log.info(kots);
}
}
@Data
class Kot implements Comparator<Kot> {
private String productName;
private Integer kotNo;
public Kot(){}
public Kot(String productName,Integer kotNo){
this.productName = productName;
this.kotNo = kotNo;
}
@Override
public int compare(Kot kot1, Kot kot2) {
int kotNoCompare = kot1.kotNo.compareTo(kot2.kotNo);
if (kotNoCompare == 0) {
int productNameCompare = kot1.productName.compareTo(kot2.productName);
return productNameCompare;
}
return kotNoCompare;
}
}
Error showing in following line 下一行显示错误
Collections.sort(kots);
Error Saying that, 说错了,
Bound mismatch: The generic method sort(List) of type Collections is not applicable for the arguments (List). 绑定不匹配:类型为Collections的通用方法sort(List)不适用于参数(List)。 The inferred type Kot is not a valid substitute for the bounded parameter >
推断的类型Kot不能有效替代边界参数>
What I'm doing wrong? 我做错了什么?
you need to implement Comparable
Interface for this. 您需要为此实现
Comparable
接口。 Eg: 例如:
class Kot implements Comparable<Kot> {
private String productName;
private Integer kotNo;
public Kot() {
}
public Kot(String productName, Integer kotNo) {
this.productName = productName;
this.kotNo = kotNo;
}
@Override
public int compareTo(Kot kot1) {
int kotNoCompare = kot1.kotNo.compareTo(this.kotNo);
if (kotNoCompare == 0) {
int productNameCompare = kot1.productName.compareTo(this.productName);
return productNameCompare;
}
return kotNoCompare;
}
}
If you want to use Comparator
only, then you have to move that comparison logic into a separate class. 如果只想使用
Comparator
,则必须将该比较逻辑移到单独的类中。 Kot
will remain a simple POJO class, containing only getter and setters. Kot
将仍然是一个简单的POJO类,仅包含getter和setter。
Eg of Comparataor
only logic: 例如,仅
Comparataor
逻辑:
class Comp implements Comparator<Kot>{
@Override
public int compare(Kot kot1, Kot kot2) {
int kotNoCompare = kot1.getKotNo().compareTo(kot2.getKotNo());
if (kotNoCompare == 0) {
int productNameCompare = kot1.getProductName().compareTo(kot2.getProductName());
return productNameCompare;
}
return kotNoCompare;
}
}
Now to sort your list, you can use other method of sorting: 现在对列表进行排序,您可以使用其他排序方法:
Collections.sort(kots, new Comp());
You made your Kot
a Comparator
of Kot
s. 你让你的
Kot
一个Comparator
的Kot
秒。 However, sort()
method expects Kot
to be Comparable
to other Kot
s: 但是,
sort()
方法期望Kot
与其他Kot
Comparable
:
class Kot implements Comparable<Kot> {
...
public int compareTo(Kot otherKot) {
// Comparison logic needs to be transformed
// to compare otherKot to this, rather than kot1 to kot2
...
}
}
You could also split your class into Kot
and KotComparator
, move comparator logic into KotComparator
, and use sort()
overload that takes a custom comparator to do sorting. 您还可以将您的类分为
Kot
和KotComparator
,将比较器逻辑移至KotComparator
,并使用sort()
重载,该重载需要使用自定义比较器进行排序。
I assume you need to sort 我认为你需要排序
kotNo
kotNo
productName
productName
Actually this Comparable
thing is an age old one. 其实,这
Comparable
东西已经Comparable
了。 With Java 8 all you need is only one line, 使用Java 8,您只需要一行,
List<Kot> kots = new ArrayList<Kot>(){{
add(new Kot("aa",1));
add(new Kot("vv",1));
add(new Kot("zz",2));
add(new Kot("bb",3));
add(new Kot("cc",1));
}};
kots = kots.stream().sorted(
comparing(Kot::getKotNo)
.thenComparing(comparing(Kot::getProductName)))
.collect(toList());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.