• java中的排序(自定义数据排序)--使用Collections的sort方法


    排序:将一组数据按相应的规则 排列 顺序

    1.规则:
          基本数据类型:日常的大小排序。
    引用类型:
    1. 内置引用类型(String,Integer..),内部已经指定规则,直接使用即可。----实现Comparable接口

        1. 整数、 Integer..:根据基本数据类型大小

        2. Character(字符):根据Unicode编码顺序

        3. String(字符串):

          1)如果其中一个是另一个起始开始的子串,返回长度之差,

          2)否则返回第一个不相等的Unicode之差。

        4. 日期:根据日期的长整型数比较。

    1. 自定义引用类型,需要按照业务规则排序。有两种方式,分别如下所述:
        当引用类型的内置排序方式无法满足需求时可以自己实现满足既定要求的排序,有两种方式:
        第一种 自定义业务排序类:新建一个业务排序类实现java.util.Comparator 下的compare 接口,然后使用java提供的Collections调用排序方法,并将此业务排序类作为参数传递给Collections的sort方法,如下:
                   (1)新建一个实体类,如下
    package top.wfaceboss.sort.refType2;
    
    public class Goods {
        // 价格
        private double price;
        // 商品名称
        private String name;
        // 收藏量
        private int fav;
    
        public Goods() {
        }
    
        public Goods(String name,double price,  int fav) {
            super();
            this.price = price;
            this.name = name;
            this.fav = fav;
        }
    
        public double getPrice() {
            return price;
        }
    
        public void setPrice(double price) {
            this.price = price;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getFav() {
            return fav;
        }
    
        public void setFav(int fav) {
            this.fav = fav;
        }
    
        @Override
        public String toString() {
            return "商品名:" + this.name + ",收藏量:" + this.fav + ",价格:" + this.price + "
    ";
        }
    
    }
    View Code

        (2)新建业务排序类(实现java.util.Comparator接口),编写符合业务要求的排序方法,如下是按照价格排序的业务类(降序)

    package top.wfaceboss.sort.refType2;
    
    /**
     * 按照价格排序的业务类(降序)
     * 
     * @author Administrator
     *
     */
    public class GoodsPriceCompare implements java.util.Comparator<Goods> {
    
        @Override
        public int compare(Goods o1, Goods o2) {
            
            return -(o1.getPrice()-o2.getPrice()>0?1:o1.getPrice()==o2.getPrice()?0:-1);//降序
        }
    
    }
    View Code

        (3)使用业务排序类

    package top.wfaceboss.sort.refType2;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    public class GoodsApp {
        public static void main(String[] args) {
            List<Goods> list = new ArrayList<Goods>();
            list.add(new Goods("老马视频", 100, 2000));
            list.add(new Goods("老高视频", 50, 2000));
            list.add(new Goods("老裴视频", 1000, 1000));
            System.out.println("排序前:" + list);
            Collections.sort(list,new GoodsPriceCompare());
            System.out.println("排序后:"+list);
        }
    }
        第二种:实体类实现 java.lang.Comparable下的compareTo接口,在接口中实现满足需求的,然后使用java提供的Collections调用排序方法sort,会自动调用此时实现的接口方法。
                 (1)新建一个实体类,实现java.lang.Comparable接口compareTo,如下:
    package top.wfaceboss.sort.refType;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    /**
     * 新闻条目实体类 排序方式: java.lang.Comparable+compareTo
     * 
     * @author Administrator
     * @param <T>
     *
     */
    public class NewsItem implements java.lang.Comparable<NewsItem> {
        // 标题
        private String title;
        // 点击量
        private int hits;
        // 时间
        private Date pubTime;
    
        public int getHits() {
            return hits;
        }
    
        public void setHits(int hits) {
            this.hits = hits;
        }
    
        public NewsItem() {
    
        }
    
        public NewsItem(String title, int hits, Date pubTime) {
            super();
            this.title = title;
            this.hits = hits;
            this.pubTime = pubTime;
        }
    
        public String getTitle() {
            return title;
        }
    
        public void setTitle(String title) {
            this.title = title;
        }
    
        public Date getPubTime() {
            return pubTime;
        }
    
        public void setPubTime(Date pubTime) {
            this.pubTime = pubTime;
        }
    
        // 时间降序+点击量升序+标题降序
        @Override
        public int compareTo(NewsItem o) {
            System.out.println("============dd==========");
            int result = 0;
            // 比较时间
            result = -this.pubTime.compareTo(o.pubTime);// 降序
            if (0 == result) {// 时间相同
                // 点击量
                result = this.hits - o.hits;// 升序
                if (0 == result) {// 点击量相同
                    // 标题
                    result = -this.title.compareTo(o.title);// 降序
                }
            }
            return result;
        }
    
        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
            sb.append("标题:").append(this.title);
            sb.append(",时间:").append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(this.pubTime));
            sb.append(",点击量:").append(this.hits).append('
    ');
            return sb.toString();
        }
    
    }
    View Code

        (2)使用java自带的Collections调用sort,对该实体类的实例进行排序:

    package top.wfaceboss.sort.refType;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Date;
    import java.util.List;
    
    /**
     * 使用Collections
     * 
     */
    public class NewsItemApp {
        public static void main(String[] args) {
            List<NewsItem> news = new ArrayList<NewsItem>();
            news.add(new NewsItem("aaa", 50, new Date()));
            news.add(new NewsItem("bbb", 60, new Date(System.currentTimeMillis() - 1000 * 60 * 60)));
            news.add(new NewsItem("ccc", 30, new Date(System.currentTimeMillis() + 1000 * 60 * 60)));
            
            Collections.sort(news);
            System.out.println(news);
        }
    }
    2.顺序:
      升序:从小到大
      降序:从大到小
    3. 排列:
      算法:如冒泡...
  • 相关阅读:
    springboot初始篇(一)
    SpringBoot使用数据库JdbcTemplate(三)
    java实现分页查询
    设计模式之单例模式
    ❤️考研数学公式❤️
    ❤️图的遍历❤️
    图的存储
    图的基本概念
    森林与二叉树的应用
    树相关的代码题
  • 原文地址:https://www.cnblogs.com/wfaceboss/p/10333088.html
Copyright © 2020-2023  润新知