Java Program to Sort ArrayList of Custom Objects By Property
Java Class and Objects.
A class in Java is an outline or model that specifies the variables and methods shared by objects of a particular kind. An object is an instance of a class and can be considered a distinct and particular illustration of a class. Fields and functions, respectively, refer to the variables and methods declared in a class.
- A class specifies the attributes and behavior that objects created from the class should have.
To know more about Java Program to Sort ArrayList of Custom Objects By Property read the complete article.
Steps for sorting Array List of Custom Objects:
Create a custom object class and implement the “Comparable” interface. This interface has one method named “compareTo” that compares two objects and returns an integer (-1, 0, or 1).
In your unique object class, override the “compareTo” method and specify the sorting logic depending on the desired sorting criteria.
To sort a list of customised objects, use the ArrayList’s “sort” method. This method compares and sorts the objects using the “compareTo” method.
- The Java Collection framework includes a dynamic data structure called an ArrayList.The elements are stored in a dynamic array using the Java ArrayList class. Similar to an array, but with no size restrictions. At any time, we can add or remove components.You can store a group of elements in a resizable array since it implements the List interface.
Let’s look at a Java Program to Sort ArrayList of Custom Objects By Property
Example 1: Sort ArrayList of Custom Objects By Property
import java.util.ArrayList; import java.util.Collections; class Employee implements Comparable{ private int id; private String name; private int age; public Employee(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } public int getId() { return id; } public String getName() { return name; } public int getAge() { return age; } @Override public int compareTo(Employee o) { return this.age - o.age; } } public class Main { public static void main(String[] args) { ArrayList employees = new ArrayList<>(); employees.add(new Employee(1, "Ayush", 22)); employees.add(new Employee(2, "Ashish", 27)); employees.add(new Employee(3, "Harsh", 36)); Collections.sort(employees); for (Employee employee : employees) { System.out.println(employee.getName() + " " + employee.getAge()); } } }
Output
Ayush 22 Ashish 27 Harsh 36
- In this example, the Employee class implements the Comparable interface and substitutes the age property comparison method for the compareTo() method. Based on the comparison performed in the compareTo() function, the Collections.sort() method arranges the ArrayList of Employee objects in ascending order.
Example 2 :Java Program to sort Array List of custom object by property.
import java.util.ArrayList; import java.util.Collections; class Person implements Comparable{ private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } @Override public int compareTo(Person o) { return this.name.compareTo(o.name); } } public class Main { public static void main(String[] args) { ArrayList people = new ArrayList<>(); people.add(new Person("Amit jain", 10)); people.add(new Person("Vaibhav arora", 15)); people.add(new Person("Rishi", 25)); Collections.sort(people); for (Person person : people) { System.out.println(person.getName() + " " + person.getAge()); } } }
Output
Amit jain 10 Rishi 25 Vaibhav arora 15
Example 3: Java Program to sort Array List of custom object by property
import java.util.*; class CustomObject { private String custom_property; public CustomObject(String property){ this.custom_property = property; } public String get_custom_property(){ return this.custom_property; } } public class Main { public static void print(ArrayListinput_list){ for (CustomObject object : input_list) { System.out.println(object.get_custom_property()); } } public static void sort(ArrayList input_list){ input_list.sort((object_1, object_2) -> object_1.get_custom_property().compareTo( object_2.get_custom_property())); } public static void add(ArrayList input_list){ input_list.add(new CustomObject("GET DRUNK")); input_list.add(new CustomObject("HIgh JUMP")); input_list.add(new CustomObject("GYM FIT")); input_list.add(new CustomObject("NEW LIFE")); } public static void main(String[] args){ System.out.println("Required list for GTA 5 Cheatsheet"); ArrayList input_list = new ArrayList<>(); add(input_list); System.out.println("GTA 5 CHEATSHEET LIST "); print(input_list); sort(input_list); System.out.println("\nTHE NEW LIST AFTER SORTING NEW CHEATSHEET: "); print(input_list); } }
Output
Required list for GTA 5 Cheatsheet GTA 5 CHEATSHEET LIST GET DRUNK HIgh JUMP GYM FIT NEW LIFE THE NEW LIST AFTER SORTING NEW CHEATSHEET: GET DRUNK GYM FIT HIgh JUMP NEW LIFE
- This Java code creates a new class called CustomObject, which includes a function Object() that sets a single private instance variable called custom property. The custom property's value is returned by the method get custom property, which is also present.
- The Main class contains the following methods:
- print: accepts an ArrayList of CustomObject and prints the value of custom_property for each CustomObject in the list.
- sort: accepts an ArrayList of CustomObject and sorts the objects in the list based on the value of custom_property using a lambda expression.
- add: accepts an ArrayList of CustomObject and adds 4 new CustomObject instances to the list.
- main: creates an instance of ArrayList of CustomObject, adds some instances to the list, prints the list, sorts it, and prints the sorted list.
- When this code is run, it will print a message "Required list for GTA 5 Cheatsheet", followed by the original list of CustomObject instances, and then the sorted list.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment