• java,对象排序


    Java对象排序有两种方法

    一,在对象类中实现接口 comparable

    package com.m01.collections;
    
    public class User implements Comparable {
        private int id;
        private String name;
        private double score;
        public User() {
            super();
        }
        public User(int id, String name, double score) {
            super();
            this.id = id;
            this.name = name;
            this.score = score;
        }
    
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public double getScore() {
            return score;
        }
        public void setScore(double score) {
            this.score = score;
        }
        @Override
        public int compareTo(Object o) {
            User user=(User) o;
            if(this.score>user.score){
                return 1;
            }
            else if(this.score<user.score){
                return -1;
            }else{
                if(this.id>user.id){
                    return 1;
                }
                else if(this.id<user.id){
                    return -1;
                }else{
                    return 0;
                }
            }
        }
        @Override
        public String toString() {
            return "User [id=" + id + ", name=" + name + ", score=" + score + "]";
        }
    
    }
    List<User> list=new ArrayList();
            list.add(new User(1, "夏末", 12));
            list.add(new User(2, "夏末2", 12));
            list.add(new User(2, "夏末", 13));
            list.add(new User(4, "夏末", 12));
            list.add(new User(0, "夏末", 12));
            Collections.sort(list);
            for(User user : list){
                System.out.println(user);
            }

    二,编写比较器 实现comparator,对象类不需要实现comparable接口

    List<Person> listp=new ArrayList();
            listp.add(new Person(1, "夏末", 1));
            listp.add(new Person(3, "夏末", 102));
            listp.add(new Person(2, "夏末", 120));
            listp.add(new Person(4, "夏末", 2));
            listp.add(new Person(0, "夏末", 0));
            Collections.sort(listp,new Comparator<Person>() {
                @Override
                public int compare(Person o1, Person o2) {
                    if(o1.getScore()>o2.getScore()){
                        return 1;
                    }else if(o1.getScore()<o2.getScore()){
                        return -1;
                    }else{
                        return 0;
                    }
                }
            });
            for(Person user:listp){
                System.out.println(user.toString());
            }
  • 相关阅读:
    14、打开MySQL数据库远程访问权限
    11、mysql导出数据库表的创建sql语句
    13、自增长主键及字符集的修改
    ASP.NET2.0 Provider模型
    ASP.NET Provider模型(3)
    ASP.NET2.0 ObjectDataSource的使用详解
    ASP.NET2.0快速入门存储用户配置文件
    ASP.NET2.0快速入门--使用母版页创建布局
    ASP.NET2.0 ObjectDataSource的使用详解(3)
    ASP.NET2.0 Provider模型(二)
  • 原文地址:https://www.cnblogs.com/m01qiuping/p/6431098.html
Copyright © 2020-2023  润新知