Java中Collections.sort()针对List排序应用

第一种:Comparable 排序接口   public class SortA implements Comparable 

public int compareTo(SortA a) {   //实现方法
     /**
       * return a.getOrder()-this.order; //升序
       * return this.order-a.getOrder(); //倒序
       * return this.order.compareTo(a.getOrder());//升序
       */
        return a.getOrder().compareTo(this.order);//倒序
}

Collections.sort(listA);


第二种:Comparator比较器接口

   Collections.sort(listA, new Comparator() {
        public int compare(SortA s1, SortA s2) {
            /**
             * 升序排的话就是第一个参数.compareTo(第二个参数);
             * 降序排的话就是第二个参数.compareTo(第一个参数);
             */
            return s2.getOrder().compareTo(s1.getOrder());
        }
     
    });

以上转自 侯爵吖 https://www.cnblogs.com/learnapi/p/9003112.html

我的实际应用

public class MonitorVideoData extends BaseData implements Comparable {

private String cameraName;
private String cameraTypeName;
private int cameraType;
private String cameraUuid;
private int exceptionType;
private int priority;
private String picUrl;
private String deviceVideoUuid;
private String platformType;  
private String platformId;
private String appKey;
private String accessToken;
private String coverPic;
private String projectId;
private String projectName;
private String prinName;
private String cameraStatus;
private String disabled;
private Integer id;
private String istop;
private String timeLapseOpen;
private String timeLapseSec;
private String deviceNum;
private String projectLogo;
private String startDate;
private String endDate;
private String videoUrl;
private String createDay;

public String getCreateDay() {
    return createDay;
}

public void setCreateDay(String createDay) {
    this.createDay = createDay;
}

public String getVideoUrl() {
    return videoUrl;
}

public void setVideoUrl(String videoUrl) {
    this.videoUrl = videoUrl;
}

public String getStartDate() {
    return startDate;
}

public void setStartDate(String startDate) {
    this.startDate = startDate;
}

public String getEndDate() {
    return endDate;
}

public void setEndDate(String endDate) {
    this.endDate = endDate;
}

public String getDeviceNum() {
    return deviceNum;
}

public void setDeviceNum(String deviceNum) {
    this.deviceNum = deviceNum;
}

public String getProjectLogo() {
    return projectLogo;
}

public void setProjectLogo(String projectLogo) {
    this.projectLogo = projectLogo;
}

public String getCameraStatus() {
    return cameraStatus;
}

public void setCameraStatus(String cameraStatus) {
    this.cameraStatus = cameraStatus;
}

public String getDisabled() {
    return disabled;
}

public void setDisabled(String disabled) {
    this.disabled = disabled;
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getIstop() {
    return istop;
}

public void setIstop(String istop) {
    this.istop = istop;
}

public String getTimeLapseOpen() {
    return timeLapseOpen;
}

public void setTimeLapseOpen(String timeLapseOpen) {
    this.timeLapseOpen = timeLapseOpen;
}

public String getTimeLapseSec() {
    return timeLapseSec;
}

public void setTimeLapseSec(String timeLapseSec) {
    this.timeLapseSec = timeLapseSec;
}

public String getPrinName() {
    return prinName;
}

public void setPrinName(String prinName) {
    this.prinName = prinName;
}

public String getCameraName() {
    return cameraName;
}

public void setCameraName(String cameraName) {
    this.cameraName = cameraName;
}

public String getCameraTypeName() {
    return cameraTypeName;
}

public void setCameraTypeName(String cameraTypeName) {
    this.cameraTypeName = cameraTypeName;
}

public int getCameraType() {
    return cameraType;
}

public void setCameraType(int cameraType) {
    this.cameraType = cameraType;
}

public String getCameraUuid() {
    return cameraUuid;
}

public void setCameraUuid(String cameraUuid) {
    this.cameraUuid = cameraUuid;
}

public int getExceptionType() {
    return exceptionType;
}

public void setExceptionType(int exceptionType) {
    this.exceptionType = exceptionType;
}

public int getPriority() {
    return priority;
}

public void setPriority(int priority) {
    this.priority = priority;
}

public String getPicUrl() {
    return picUrl;
}

public void setPicUrl(String picUrl) {
    this.picUrl = picUrl;
}

public String getDeviceVideoUuid() {
    return deviceVideoUuid;
}

public void setDeviceVideoUuid(String deviceVideoUuid) {
    this.deviceVideoUuid = deviceVideoUuid;
}

public String getPlatformType() {
    return platformType;
}

public void setPlatformType(String platformType) {
    this.platformType = platformType;
}

public String getPlatformId() {
    return platformId;
}

public void setPlatformId(String platformId) {
    this.platformId = platformId;
}

public String getAppKey() {
    return appKey;
}

public void setAppKey(String appKey) {
    this.appKey = appKey;
}

public String getAccessToken() {
    return accessToken;
}

public void setAccessToken(String accessToken) {
    this.accessToken = accessToken;
}

public String getCoverPic() {
    return coverPic;
}

public void setCoverPic(String coverPic) {
    this.coverPic = coverPic;
}

public String getProjectId() {
    return projectId;
}

public void setProjectId(String projectId) {
    this.projectId = projectId;
}

public String getProjectName() {
    return projectName;
}

public void setProjectName(String projectName) {
    this.projectName = projectName;
}

@Override
public int compareTo(@NonNull MonitorVideoData o) {
    if (!StrUtil.isEmptyOrNull(this.deviceNum) && !StrUtil.isEmptyOrNull(o.deviceNum)) {
        return Integer.valueOf(this.deviceNum) - Integer.valueOf(o.deviceNum);
    }
    return 0;
  }
}
 
降序  Collections.sort(projectList, Collections.reverseOrde());
升序  Collections.sort(projectList);

你可能感兴趣的:(Java中Collections.sort()针对List排序应用)