How to sort a List using Comparator

In Java 8, we will organize the sorting method of List.

Java 7 and below

Review of the Java 7 era

List<String> nameList = Arrays.asList("Takahashi", "Tanaka", "Suzuki");
Collections.sort(nameList);

Java 8 or later

Starting with Java 8, sort (Comparator) has been added to List and you can use it to sort the list.

nameList.sort(Comparator.comparingInt(String::length));

Sort by multiple conditions (string length-> alphabetical order). You can add sort conditions by using thenComparing ().

nameList.sort(Comparator.comparingInt(String::length)
	.thenComparing(Comparator.naturalOrder()));

Also, with the introduction of Stream, you can source lists with Stream # sorted.

List<String> sortedList = nameList.stream().sorted()
		.collect(Collectors.toList());

Using Lambda

Sort by string length.

List<String> sortedList = nameList.stream().sorted((s1, s2) -> s1.length() - s2.length())
		.collect(Collectors.toList());

Sort alphabetically.

List<String> sortedList = nameList.stream().sorted((s1, s2) -> s1.compareTo(s2))
		.collect(Collectors.toList())

Use of Comparator

Sort by string length.

List<String> sortedList = nameList.stream().sorted(
		Comparator.comparingInt(String::length)).collect(Collectors.toList());

Sort alphabetically.

List<String> sortedList = nameList.stream().sorted(
		Comparator.naturalOrder()).collect(Collectors.toList())

Sorting multiple conditions with Comparator

String length-> Sort alphabetically. You can add sort conditions by using thenComparing ().

List<String> sortedList = nameList.stream().sorted(
		Comparator.comparingInt(String::length)
		.thenComparing(Comparator.naturalOrder())).collect(Collectors.toList());

Sorting the object list in Comparator

List<Person> sortedList = personList.stream().sorted(new Comparator<Person>(){

	@Override
	public int compare(Person o1, Person o2) {
		return o1.getName().compareTo(o2.getName());
	}
			
}).collect(Collectors.toList());

Recommended Posts

How to sort a List using Comparator
How to execute a contract using web3j
How to sort the List of SelectItem
[Rails] How to create a graph using lazy_high_charts
How to delete a controller etc. using a command
[Ethereum] How to execute a contract using web3j-Part 2-
How to generate a primary key using @GeneratedValue
[Java] How to operate List using Stream API
java: How to write a generic type list [Note]
How to leave a comment
How to delete custom Adapter elements using a custom model
How to convert A to a and a to A using AND and OR in Java
How to insert a video
How to create a method
[Rails] How to install a decorator using gem draper
How to authorize using graphql-ruby
How to output array values without using a for statement
How to join a table without using DBFlute and sql
How to register as a customer with Square using Tomcat
How to add columns to a table
How to make a Java container
How to sign a Minecraft MOD
How to make a JDBC driver
Sort a List of Java objects
[Java] How to create a folder
How to write a ternary operator
[Swift] How to send a notification
How to make a splash screen
How to make a Jenkins plugin
[Java] How to use List [ArrayList]
How to make a Maven project
How to make a Java array
How to build CloudStack using Docker
How to create a jar file or war file using the jar command
How to make a hinadan for a Spring Boot project using SPRING INITIALIZR
How to run a mock server on Swagger-ui using stoplight/prism (using AWS/EC2/Docker)
[Rails 6] How to create a dynamic form input screen using cocoon
How to output a list of strings in JSF as comma-separated strings
How to make a groundbreaking diamond using Java for statement wwww
[Xcode] How to add a README.md file
How to make a Java calendar Summary
Why assign an ArrayList to a List
A memorandum on how to use Eclipse
How to redo a deployment on Heroku
[Rails] How to upload images using Carrierwave
[Basic] How to write a Dockerfile Self-learning ②
How to insert a video in Rails
[Java] How to add data to List (add, addAll)
[Java] How to calculate age using LocalDate
How to add a new hash / array
[Introduction to Java] How to write a Java program
How to create a Maven repository for 2020
How to make a Discord bot (Java)
How to print a Java Word document
[Swift5] How to create a splash screen
[rails] How to create a partial template
[Swift5] How to implement animation using "lottie-ios"
How to implement image posting using rails
Ability to display a list of products
How to make asynchronous pagenations using Kaminari
How to publish a library in jCenter