Sort a List of Java objects

Sort with Collections.sort () using Comparator

--Comparator allows you to sort by a specific value of your own class object.

python


//List<Person>Sort the list foo by x field
Collections.sort(
                  foo,
                  new Comparator<Person>() {
                    @Override
                    public int compare(Person obj1, Person obj2){
                      return obj2.getX - obj1.getX;
                       }
                    }
                  );

Use Comparator.comparing

--Comparator.comparing allows you to write neatly

python


foo.sort(Comparator.comparing(Person::getX));

Recommended Posts

Sort a List of Java objects
[java] sort in list
Java List Group, Sort, etc.
Get a list of MBean information for Java applications
Extract a specific element from the list of objects
Think of a Java update strategy
[Java] Delete the elements of List
A list of rawValues for UITextContentType.
List of members added in Java 9
A brief description of JAVA dependencies
List of types added in Java 9
Sort List in descending order in Java and generate a new List non-destructively
Cast an array of Strings to a List of Integers in Java
Name a group of regular expressions (Java)
[Java] Write a sort faster than Arrays.sort
Easily measure the size of Java Objects
List of download destinations for oracle java
How to sort the List of SelectItem
Ability to display a list of products
[Java] Contents of Collection interface and List interface
java bubble sort
Get a list of S3 files with ListObjectsV2Request (AWS SDK for Java)
Java memorandum (list)
Clone Java List.
java selection sort
java insertion sort
[Java] Overview of Java
About List [Java]
Do you need a memory-aware implementation of Java?
Measure the size of a folder in Java
Create Scala Seq from Java, make Scala Seq a Java List
[Note] Java: Speed of List processing by purpose
A collection of simple questions for Java beginners
A quick review of Java learned in class
[Java] Sort ArrayList with elements of your own class
Expired collection of java
Predicted Features of Java
[Java] Significance of serialVersionUID
A quick review of Java learned in class part4
java classes, instances, objects
[Java] Create a filter
[Java] Sort the list using streams and lambda expressions
NIO.2 review of java
Review of java Shilber
java: How to write a generic type list [Note]
Browse class objects in Kotlin (instead of Java class name.class)
java --Unification of comments
java build a triangle
Java Japanese (Kanji) Sort
List of beginners (List) memo
Awesome Java: A number of great Java framework library software
Experience of passing Java Silver as a new graduate
Let's make a robot! "A simple demo of Java AWT Robot"
Get a list of classes in a Guava specific package
About Java Array List
History of Java annotation
The objects in the List were references, right? Confirmation of
java (merits of polymorphism)
Source of cellular objects
Output a list of cron settings for all users.
[Rails] Volume that displays favorites and a list of favorites