List排序Collections.sort()
Posted 三里路
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了List排序Collections.sort()相关的知识,希望对你有一定的参考价值。
方式一: 使用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}]
以上是关于List排序Collections.sort()的主要内容,如果未能解决你的问题,请参考以下文章
collections.sort对javabean的list进行排序
Java语言利用Collections.sort对Map,List排序
java Collections.sort()实现List排序的默认方法和自定义方法
Collections.sort详解
List字符串和数字用Collections.sort排序
Java Collections.sort方法对list集合排序