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 How to Convert an ArrayList of Objects to a JSON Array in Java? In Java, an ArrayList is a resizable array implementation of the List interface. It implements the List interface and is the most commonly used implementation of List. In this article, we will learn how to convert an ArrayList of objects to a JSON array in Java. Steps to Convert an ArrayList of Obje 3 min read How to Add Custom Class Objects to the TreeSet in Java? TreeSet is an implementation of the SortedSet interface in java that uses a red-black tree for storage. By default, It maintains an ascending order. It contains unique elements only. It doesn't allow null elements. Access and retrieval times are quite fast. To add the user-defined object into TreeSe 3 min read Sort an array of pairs using Java Arrays.sort() with custom Comparator Given an array of pairs of integers. The task is to sort the array with respect to the second element of the pair.Example:Input: [(10, 20), (20, 30), (5, 6), (2, 5)] Output: [(2, 5), (5, 6), (10, 20), (20, 30)]Program of Java Array Sort Custom ComparatorJava// Java code to sort the array // accordin 2 min read Java Program to Sort Names in an Alphabetical Order For, sorting names in an Alphabetical order there are multiple ways to sort the array, like using inbuilt Arrays.sort() method or using normal sorting algorithms like the bubble sort, merge sort. Here let's use the bubble sort and inbuilt sort. Example: Input : Array[] = {"Sourabh", "Anoop, "Harsh", 3 min read Java Program to Sort the Elements of an Array in Ascending Order Here, we will sort the array in ascending order to arrange elements from smallest to largest, i.e., ascending order. So the easy solution is that we can use the Array.sort method. We can also sort the array using Bubble sort.1. Using Arrays.sort() MethodIn this example, we will use the Arrays.sort() 2 min read Like