我需要使用多个属性对列表进行排序,我尝试了这段代码但是我收到了编译错误
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;
}
}
错误显示在以下行
中 Collections.sort(kots);
错误说,
绑定不匹配:类型集合的通用方法sort(List)不适用于参数(List)。推断类型Kot不是有界参数&gt;
的有效替代我做错了什么?
答案 0 :(得分:4)
您需要为此实现Comparable
接口。
E.g:
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;
}
}
如果您只想使用Comparator
,则必须将该比较逻辑移动到单独的类中。 Kot
将仍然是一个简单的POJO类,只包含getter和setter。
仅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;
}
}
现在要对列表进行排序,您可以使用其他排序方法:
Collections.sort(kots, new Comp());
答案 1 :(得分:2)
您已Kot
Comparator
Kot
个sort()
。但是,Kot
方法希望Comparable
为Kot
到其他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
...
}
}
s:
Kot
您还可以将您的班级拆分为KotComparator
和KotComparator
,将比较器逻辑移至sort()
,并使用import networkx as nx
import pylab as plt
from networkx.drawing.nx_agraph import graphviz_layout
G = nx.DiGraph()
G.add_node(1,level=1)
G.add_node(2,level=2)
G.add_node(3,level=2)
G.add_node(4,level=3)
G.add_edge(1,2)
G.add_edge(1,3)
G.add_edge(2,4)
nx.draw(G, pos=graphviz_layout(G), node_size=1600, cmap=plt.cm.Blues,
node_color=range(len(G)),
prog='dot')
plt.show()
重载,使自定义比较器进行排序。< / p>
答案 2 :(得分:0)
我假设您需要排序
--Query first selects original column as well as replacement string and then update original column
Update Tbl1
Set Tbl1.post_content=Tbl2.Replacement
From le_wp_posts as Tbl1
Inner Join
(
select post_content,REPLACE(post_content,'Â','') as Replacement
from le_wp_posts
where post_content like '%Â%'
) as Tbl2
On Tbl1.post_content=Tbl2.post_content
kotNo
实际上这个productName
的东西是一个古老的东西。使用Java 8,您只需要一行,
Comparable