Java Program to Sort ArrayList of Custom Objects By Property Last Updated : 17 Nov, 2020 Comments Improve Suggest changes Like Article Like Report Here we are going to look at the approach of sorting an ArrayList of custom objects by using a property. Approach: 1. Create a getter function which returns the value stored in the class variable. 2. Create a list and use sort() function which takes the values of the list as arguments and compares them with compareTo() method. 3. This function will then return a positive number if the first argument's property is greater than the second's, negative if it is less and zero if they are equal. Implementation Java // Java Program to Sort ArrayList of Custom Objects By // Property import java.util.*; public class Main { private String value; public Main(String val) { this.value = val; } // Defining a getter method public String getValue() { return this.value; } // list of Main objects static ArrayList<Main> list = new ArrayList<>(); public static void sortList(int length) { // Sorting the list using lambda function list.sort( (a, b) -> a.getValue().compareTo(b.getValue())); System.out.println("Sorted List : "); // Printing the sorted List for (Main obj : list) { System.out.println(obj.getValue()); } } public static void main(String[] args) { // take input Scanner sc = new Scanner(System.in); System.out.print( "How many characters you want to enter : "); int l = sc.nextInt(); // Taking value of list as input for (int i = 0; i < l; i++) { list.add(new Main(sc.next())); } sortList(); } } Output Comment More infoAdvertise with us Next Article Java Program to Sort ArrayList of Custom Objects By Property rbbansal Follow Improve Article Tags : Java Java Programs Java-ArrayList Practice Tags : Java Similar Reads How to Sort an ArrayList of Objects by Property in Java? ArrayList in Java (equivalent to vector in C++) having a dynamic size. It can be shrinked or expanded based on size. ArrayList is a part of the collection framework and is present in java.util package. --> java.util Package --> ArrayList Class Syntax: Creating an empty ArrayList ArrayList < 7 min read Java Program to Sort Objects in ArrayList by Date The foremost tool that strikes is the sort() method to be used for the comparator mechanism of the Collections class which sorts in the decreasing order. Yes if in generic we want to achieve the goal considering the boundary condition where objects to sorted are user-defined then blindly do with Com 6 min read Java Program to Sort a HashMap by Keys and Values HashMap<K, V> is a Java Collection and is a part of java.util package. It provides the basic implementation of the Map interface of Java. It stores the data in the form of Key, Value pairs, where the keys must be unique but there is no restriction for values. If we try to insert the duplicate 3 min read Java Program for Menu Driven Sorting of Array In Java, sorting an array consists of arranging the elements in a particular order, such as ascending or descending. This can be achieved using various algorithms like Bubble Sort, Selection Sort, or Insertion Sort. A menu-driven program allows users to select the desired sorting method dynamically. 7 min read How to Sort ArrayList using Comparator? Comparator is an interface that is used for rearranging the Arraylist in a sorted manner. Comparator is used to sort an ArrayList of User-defined objects. In java, Comparator is provided in java.util package. Using Comparator we can sort ArrayList on the basis of multiple variables. We can simply im 3 min read Like