Home  >  Article  >  Java  >  How to sort objects of a class in java

How to sort objects of a class in java

王林
王林forward
2019-12-11 17:51:453197browse

How to sort objects of a class in java

We need to sort the objects of the class according to a certain attribute (or multiple attributes) in the class. There are two ways to achieve this. One method is class implementation. Comparable8742468051c85b06f0a0af9e3e506b5c interface, and then call the Collections.sort(List) method to sort. The other method is that the class does not implement the Comparable8742468051c85b06f0a0af9e3e506b5c interface, When sorting, use the Collections.sort(List, Comparator8742468051c85b06f0a0af9e3e506b5c) method and implement the Comparator8742468051c85b06f0a0af9e3e506b5c interface.

Free learning video tutorial recommendation: java video tutorial

First create a simple student class:

public class Student {
    
    private String name;
    
    private int age;
 
    public Student() {}
 
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public int getAge() {
        return age;
    }
 
    public void setAge(int age) {
        this.age = age;
    }
}

1. Implement Comparable

public class Student implements Comparable<Student>{
 
    private String name;
 
    private int age;
 
    public Student() {}
 
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public int getAge() {
        return age;
    }
 
    public void setAge(int age) {
        this.age = age;
    }
 
    /**
     * 将对象按姓名字典序升序排序
     * @param o
     * @return
     */
    @Override
    public int compareTo(Student o) {
        return this.name.compareTo(o.getName());
    }
 
    @Override
    public String toString() {
        return "Student{" +
                "name=&#39;" + name + &#39;\&#39;&#39; +
                ", age=" + age +
                &#39;}&#39;;
    }
}

2. Sort by implementing the Comparable8742468051c85b06f0a0af9e3e506b5c interface in the Collections.sort() method

public class Client {
 
    public static void main(String[] args){
        List<Student> students = new ArrayList<>();
        students.add(new Student("a", 18));
        students.add(new Student("c", 19));
        students.add(new Student("b", 20));
 
        Collections.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getAge()>o2.getAge()? -1:(o1.getAge()==o2.getAge()? 0:1);
            }
        });
        for(Student student:students){
            System.out.println(student.toString());
        }
    }
 
}

Recommended related article tutorials:Introduction to java programming

The above is the detailed content of How to sort objects of a class in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete
Previous article:Java thread Timer timerNext article:Java thread Timer timer