方式一: 使用public static <T extends Comparable<? super T>> void sort(List<T> list)方法对list进行排序:
在要排序的list对象中 implements Comparable 并重写compareTo(方法)
public class ClickInfo implements Comparable<ClickInfo>{
private Integer cid;
private String imageId;
private Long clickCount;
private Long updateTime;
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getImageId() {
return imageId;
}
public void setImageId(String imageId) {
this.imageId = imageId;
}
public Long getClickCount() {
return clickCount;
}
public void setClickCount(Long clickCount) {
this.clickCount = clickCount;
}
public Long getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Long updateTime) {
this.updateTime = updateTime;
}
@Override
public String toString() {
return "ClickInfo{" +
"cid=" + cid +
", imageId=‘" + imageId + ‘\‘‘ +
", clickCount=" + clickCount +
", updateTime=" + updateTime +
‘}‘;
}
@Override
public int compareTo(ClickInfo o) {
/*从小到大*/
return this.getClickCount().compareTo(o.getClickCount());
/*从大到小*/
//return o.getClickCount().compareTo(this.getClickCount());
}
}
测试:
输出结果:从小到大:[ClickInfo{cid=1, imageId=‘a‘, clickCount=1, updateTime=null}, ClickInfo{cid=1, imageId=‘b‘, clickCount=3, updateTime=null}]
从大到小: [ClickInfo{cid=1, imageId=‘b‘, clickCount=3, updateTime=null}, ClickInfo{cid=1, imageId=‘a‘, clickCount=1, updateTime=null}]
方式二:使用重载方法:public static <T> void sort(List<T> list, Comparator<? super T> c)
public class ClickInfo{
private Integer cid;
private String imageId;
private Long clickCount;
private Long updateTime;
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getImageId() {
return imageId;
}
public void setImageId(String imageId) {
this.imageId = imageId;
}
public Long getClickCount() {
return clickCount;
}
public void setClickCount(Long clickCount) {
this.clickCount = clickCount;
}
public Long getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Long updateTime) {
this.updateTime = updateTime;
}
@Override
public String toString() {
return "ClickInfo{" +
"cid=" + cid +
", imageId=‘" + imageId + ‘\‘‘ +
", clickCount=" + clickCount +
", updateTime=" + updateTime +
‘}‘;
}
}
测试:
public static void main(String[] args){
List<ClickInfo> clickInfos = new ArrayList<>();
ClickInfo clickInfo = new ClickInfo();
clickInfo.setCid(1);
clickInfo.setImageId("b");
clickInfo.setClickCount((long) 3);
ClickInfo clickInfo1 = new ClickInfo();
clickInfo1.setCid(1);
clickInfo1.setImageId("a");
clickInfo1.setClickCount((long) 1);
clickInfos.add(clickInfo);
clickInfos.add(clickInfo1);
Collections.sort(clickInfos, new Comparator<ClickInfo>() {
@Override
public int compare(ClickInfo arg0, ClickInfo arg1) {
/*从小到大*/
return arg0.getClickCount().compareTo(arg1.getClickCount());
/*从大到小*/
//return arg1.getClickCount().compareTo(arg0.getClickCount());
}
});
System.out.println(clickInfos.toString());
}
输出结果:从小到大: [ClickInfo{cid=1, imageId=‘a‘, clickCount=1, updateTime=null}, ClickInfo{cid=1, imageId=‘b‘, clickCount=3, updateTime=null}]
从大到小: [ClickInfo{cid=1, imageId=‘b‘, clickCount=3, updateTime=null}, ClickInfo{cid=1, imageId=‘a‘, clickCount=1, updateTime=null}]