Java教程

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

本文主要是介绍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");

需求:

 

代码实现:

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'}

 

这篇关于Java用CompareTo方法实现根据两个或多个属性对对象进行排序的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!