[Java] Sort ArrayList with elements of your own class

First self-made class

public class Employee {
	
	private String name;
    private int age;
    
    public Employee(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;
	}
}

Put elements in list and sort

public class Array {

	public static void main(String[] args) {
			
		List<Employee> list = new ArrayList<Employee>();
		list.add(new Employee("tanaka", 25));
		list.add(new Employee("yamada", 28));
		list.add(new Employee("suzuki", 20));
			
		Comparator<Employee> compare = Comparator.comparing(Employee::getAge);
		list.sort(compare);
			
		for (Employee e : list) {
			System.out.println(e.getName() + " : " + e.getAge());
		}
	}
}

The output result is as follows

suzuki : 20 tanaka : 25 yamada : 28

In ascending order, the second argument of Comparator.comparing () can be omitted In descending order, it is necessary to specify Comparator.reverseOrder () as the second argument.

Comparator<Employee> compare = Comparator.comparing(Employee::getAge,Comparator.reverseOrder());

Recommended Posts

[Java] Sort ArrayList with elements of your own class
Call your own class created under lib with Rails
[Java] Object operation of ArrayList class (AOJ ④ Inversion of sequence)
Summarize the additional elements of the Optional class in Java 9
Sort strings functionally with java
[Java] Comparator of Collection class
Create your own Java annotations
Summary of Java Math class
Bubble sort using ArrayList (JAVA)
Java: Start WAS with Docker and deploy your own application
Create an immutable class with JAVA
[Java] Delete the elements of List
Make your own sampler with JMeter
Various methods of Java String class
Sort a List of Java objects
Make your own persistence FW (Java)
Handle your own annotations in Java
Let's express the result of analyzing Java bytecode with a class diagram
Java: Try implementing your own comma-separated formatter
[Java] Get List / Map elements with Iterator
Examine the memory usage of Java elements
Create your own validator with Bean Validation
Understand java interface in your own way
Utilization of Talend component (5) Create your own component
[Java] Inheritance and structure of HttpServlet class
Compare the elements of an array (Java)
[Java] Get the date with the LocalDateTime class
Increment with the third argument of iterate method of Stream class added from Java9
ArrayList class
Contemplation: How to take advantage of functional interfaces in your own functions (java)
Add class only to specific elements with V-for
Java method call from RPG (method call in own class)
[Java] Explanation of Strategy pattern (with sample code)
Create your own Android app for Java learning
Calculate the similarity score of strings with JAVA
Add your own authentication items with Spring Security
A quick review of Java learned in class
Create your own Utility with Thymeleaf with Spring Boot
Java Repository of Eclipse with Maven: Missing artifact ~
First touch of the Files class (or Java 8)
(Java) How to implement equals () for a class with value elements added by inheritance
Extract elements by doing regular expression replacement from a lot of HTML with java