---
title: "Sort ArrayList of custom Objects by property"  
description: "Sort ArrayList of custom Objects by property"  
author: "Anonymous User"  
published: 2015-07-10  
updated: 2015-07-10  
canonical: https://www.mindstick.com/forum/23332/sort-arraylist-of-custom-objects-by-property  
category: "java"  
tags: ["java", "collection", "array list"]  
reading_time: 1 minute  

---

# Sort ArrayList of custom Objects by property

I read about [sorting](https://www.mindstick.com/articles/1581/sorting-list-with-side-bar) [ArrayLists](https://www.mindstick.com/forum/33734/most-efficient-way-to-simultaneously-sort-three-arraylists-in-java) using a Comparator but in all of the examples [people](https://www.mindstick.com/news/2295/more-people-need-to-monitor-this-crucial-metric-for-heart-health) used compareTo which according to some [research](https://www.mindstick.com/articles/311609/how-to-research-the-companies-before-applying-to-their-open-positions) is a method for Strings.\
I wanted to sort an [ArrayList](https://www.mindstick.com/articles/11973/arraylist-class-in-c-sharp) of [custom objects](https://answers.mindstick.com/qa/34009/truncating-custom-objects-in-salesforce) by one of their [properties](https://www.mindstick.com/articles/23331/nootropics-7-different-types-and-their-unique-properties): a Date object (getStartDay()). Normally I [compare](https://www.mindstick.com/forum/2036/propertyinfo-getvalue-unable-to-compare-datetimes) them by item1.getStartDate().before(item2.getStartDate()) so I was wondering whether I could write something like:

```
public class customComparator {    public boolean compare(Object object1, Object object2) {        return object1.getStartDate().before(object2.getStartDate());    }}public class randomName {    ...    Collections.sort(Database.arrayList, new customComparator);    ...}
```

## Replies

### Reply by Anonymous User

With Java 8 you can use a method reference for your comparator:

```
import static java.util.Comparator.comparing;Collections.sort(list, comparing(MyObject::getStartDate));\
```

\
JAVA 8 lambda expression

```
Collections.sort(studList, (Student s1, Student s2) ->{        return s1.getFirstName().compareToIgnoreCase(s2.getFirstName());});
```


---

Original Source: https://www.mindstick.com/forum/23332/sort-arraylist-of-custom-objects-by-property

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
