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

How to sort objects of a class in java

王林
Release: 2019-12-11 17:51:45
forward
3207 people have browsed it

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. Comparable<T> interface, and then call the Collections.sort(List) method to sort. The other method is that the class does not implement the Comparable<T> interface, When sorting, use the Collections.sort(List, Comparator<T>) method and implement the Comparator 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;
    }
}
Copy after login

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;;
    }
}
Copy after login

2. Sort by implementing the Comparable<T> 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());
        }
    }
 
}
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template