zoukankan      html  css  js  c++  java
  • 关于自定义 List集合排序的方法!

    大致流程:

    排序是用到排序的接口Comparator<T>
    你要先建一个类实现比较器Comparator

    //大致流程
    public class StuComp implements Comparator<Student> { public int compare(Student o1, Student o2) { return o1.getName().compareToIgnoreCase(o2.getName()); } }

    然后在Collections.sort(list);的时候加上这个排序比较器
    Collections.sort(list,new StuComp());结果就对了。

    举例:有一个关于消息的实现类,根据消息的时间从大排到小,根据消息的是否已读让未读的排前,已读的拍后

    消息的实现类:

    package com.educationcrm.model;
    
    /**
     * Created by laobiao on 2016/5/21.
     * 消息列表对象
     */
    public class NewsListModel {
        private int NewsId;//消息ID
        private String sendName;//发送人姓名
        private String title;//消息的标题
        private String day;//消息发送时间
        private String idRead;//是否已读
        private String type;//消息类型
    
        public NewsListModel(int newsId, String sendName, String title, String day, String idRead, String type) {
            NewsId = newsId;
            this.sendName = sendName;
            this.title = title;
            this.day = day;
            this.idRead = idRead;
            this.type = type;
        }
    
        public int getNewsId() {
            return NewsId;
        }
    
        public void setNewsId(int newsId) {
            NewsId = newsId;
        }
    
        public String getSendName() {
            return sendName;
        }
    
        public void setSendName(String sendName) {
            this.sendName = sendName;
        }
    
        public String getTitle() {
            return title;
        }
    
        public void setTitle(String title) {
            this.title = title;
        }
    
        public String getDay() {
            return day;
        }
    
        public void setDay(String day) {
            this.day = day;
        }
    
        public String getIdRead() {
            return idRead;
        }
    
        public void setIdRead(String idRead) {
            this.idRead = idRead;
        }
    
        public String getType() {
            return type;
        }
    
        public void setType(String type) {
            this.type = type;
        }
    }

    这里是两个排序方法;

    //日期比较
    class dayComparator implements Comparator<NewsListModel>{
    
    
        @Override
        public int compare(NewsListModel o1, NewsListModel o2) {
           if(o1==null){
               return -1;
           }
            if(o2==null){
                return -1;
            }
            SimpleDateFormat sim = new SimpleDateFormat("MM月dd日 HH:mm");
            try {
                long o1date=sim.parse(o1.getDay()).getTime();
                long o2date=sim.parse(o2.getDay()).getTime();
                if(o1date>o2date){
                    return -1;
                }else {
                    return 1;
                }
    
            } catch (ParseException e) {
                e.printStackTrace();
            }
    
            return 0;
        }
    }
    
    //是否已读比较,通过判断他们的已读属性来返回值
    class isReadComparator implements Comparator<NewsListModel>{
    
    
        @Override
        public int compare(NewsListModel o1, NewsListModel o2) {
            if(o1==null){
                return -1;
            }
            if(o2==null){
                return 1;
            }
    
            if(o1.getIdRead()=="yes" && o2.getIdRead()=="no"){
                return 1;
            }
            if (o1.getIdRead()=="no" && o2.getIdRead()=="yes"){
                return -1;
            }
            return 0;
    
        }

    下面是实现方法:

     Collections.sort(a,new dayComparator());//先进行日期排序
     Collections.sort(a,new isReadComparator());//再进行时间排序

    用到的类和方法:

    排序工具类:Comparator<T>;

    排序判断方法:compare; 返回值为1时,第二个参数会排在第一个参数前面,-1则第一个参数排前面,0时则不排序;

    调用排序方法:sort(<T>object,Comparator<T> x);

      

    
    

    新建一个Comparator<T>的实现类,在类中重写compare()方法;再使用list.sort()方法来调用这个实现类,即可实现排序;

  • 相关阅读:
    继承性03
    继承性
    Arrays与Math类
    Static关键字
    random模块,time模块,os模块,sys模块
    re模块
    冒泡排序、递归、二分查找
    内置函数
    生成器和生成器表达式
    迭代器
  • 原文地址:https://www.cnblogs.com/laobiao/p/5515525.html
Copyright © 2011-2022 走看看