Java用CompareTo方法实现根据两个或多个属性对对象进行排序


CompareTo方法

CompareTo是String类的方法,CompareTo(Object o1,Object o2),就是用o1和o2进行比较

o1.compateTo(o2) 大于0 则o1大

o1.compateTo(o2) 小于0 则o2大

o1.compateTo(o2) 等于0 则一样大

升序降序

升序:

    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("111");
        list.add("222");
        list.add("333");
        Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                if(o1.compareTo(o2)>0){
                    return 1;
                }
                return -1;
            }
        });
        System.out.println(list);
    }

结果:

[111, 222, 333]

降序:

    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("111");
        list.add("222");
        list.add("333");
        Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                if(o1.compareTo(o2)<0){
                    return 1;
                }
                return -1;
            }
        });
        System.out.println(list);
    }

结果:

[333, 222, 111]

根据对象两个属性排序实现方法

Student对象三个属性:id、username、age,要求:要根据username升序、age降序的方法对student进行排序

        Student stu1 = new Student(1, "333", "333");
        Student stu2 = new Student(2, "111", "111");
        Student stu3 = new Student(3, "333", "222");
        Student stu4 = new Student(4, "222", "111");
        Student stu5 = new Student(5, "333", "111");

需求:

Java用CompareTo方法实现根据两个或多个属性对对象进行排序

 

代码实现:

Student:

public class Student {
    private int id;
    private String username;
    private String age;

    public Student(int id, String username, String age) {
        this.id = id;
        this.username = username;
        this.age = age;
    }

    public int getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", username='" + username + '/'' +
                ", age='" + age + '/'' +
                '}';
    }
}

实现代码:

    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>(6);
        Student stu1 = new Student(1, "333", "333");
        Student stu2 = new Student(2, "111", "111");
        Student stu3 = new Student(3, "333", "222");
        Student stu4 = new Student(4, "222", "111");
        Student stu5 = new Student(5, "333", "111");
        list.add(stu1);
        list.add(stu2);
        list.add(stu3);
        list.add(stu4);
        list.add(stu5);
        Collections.sort(list, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                if(o1.getUsername().compareTo(o2.getUsername())>0){
                    return 1;
                }else if(o1.getUsername().compareTo(o2.getUsername())==0){
                    if(o1.getAge().compareTo(o2.getAge())<0){
                        return 1;
                    }
                }
                return -1;
            }
        });
        for (Student stu:list) {
            System.out.println(stu);
        }
    }

 

结果:

Student{id=1, username='111', age='111'}
Student{id=2, username='222', age='111'}
Student{id=3, username='333', age='333'}
Student{id=4, username='333', age='222'}
Student{id=5, username='333', age='111'}

 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/288004.html

(0)
上一篇 2022年9月6日
下一篇 2022年9月6日

相关推荐

发表回复

登录后才能评论