How to sort an ArrayList of custom object by property in Java

0 votes
I want to sort an ArrayList of custom object by Date object property. How can I do it?
Jun 4, 2018 in Java by Daisy
• 8,140 points
3,034 views

2 answers to this question.

0 votes

As we are using Date property of ArrayList, we can use compareTo() method, because Date implements comparable.

public class CustomComparator implements Comparator<MyObject> {
    @Override
    public int compare(MyObject o1, MyObject o2) {
        return o1.getStartDate().compareTo(o2.getStartDate());
    }
}

comapreTo() returns int value.

Collections.sort(Database.arrayList, new CustomComparator());
answered Jun 4, 2018 by Parth
• 4,640 points
0 votes

You can Sort using java 8

yourList.sort(Comparator.comparing(Classname::getName));

or

yourList.stream().forEach(a -> a.getBObjects().sort(Comparator.comparing(Classname::getValue)));
answered Aug 14, 2018 by samarth295
• 2,220 points

Related Questions In Java

0 votes
2 answers
0 votes
2 answers

How an object array can be converted to string array in java?

System.arraycopy is the most efficient way, but ...READ MORE

answered Aug 8, 2018 in Java by Sushmita
• 6,920 points
5,656 views
0 votes
2 answers

In Java, what is the best way to determine the size of an object?

I happened to find a java class "jdk.nashorn.internal.ir.debug.ObjectSizeCalculator", ...READ MORE

answered Aug 19, 2019 in Java by Sirajul
• 59,230 points
13,069 views
0 votes
0 answers

How to convert an Object {} to an Array [] of key-value pairs in JavaScript

I'd want to transform the following object: {"1":5,"2":7,"3":0,"4" ...READ MORE

Sep 28, 2022 in Java by Nicholas
• 7,760 points
1,124 views
0 votes
1 answer

Sort ArrayList of custom Objects by property

Since Date implements Comparable, it has a compareTo method just like String does. So your ...READ MORE

answered Dec 22, 2020 in Java by Gitika
• 65,770 points
1,487 views
0 votes
2 answers

How to convert an array to arraylist?

In Java 9 you can use: List<String> list= List.of("Hello", "World", ...READ MORE

answered Aug 1, 2018 in Java by samarth295
• 2,220 points
1,053 views
0 votes
1 answer

How can I Sort an ArrayList in Java

You can sort the ArrayList in 2 ...READ MORE

answered Apr 23, 2018 in Java by sharth
• 3,370 points
861 views
0 votes
2 answers

How can I invoke a method when the method name is in the form of a given string?

You could probably use method invocation from reflection: Class<?> ...READ MORE

answered Aug 19, 2019 in Java by Sirajul
• 59,230 points
3,068 views
0 votes
2 answers

One line initialization of an ArrayList object in Java

In Java 8 or earlier: List<String> string = ...READ MORE

answered Jul 26, 2018 in Java by samarth295
• 2,220 points
4,757 views
0 votes
3 answers

How to sort an array in java?

import java.util.Arrays; public class Sort { ...READ MORE

answered Aug 24, 2018 in Java by Parth
• 4,640 points
1,209 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP