Sort ArrayList of Custom Objects by Property in Java



In this article, we will learn to sort ArrayList of custom objects by property in Java. The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed.

Problem Statement

ArrayList is created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk.

Below is a demonstration of the same ?

Input ?

The list is defined as
Java
Scala
Python
Mysql

Output  ?

The list after sorting values:
Java
Mysql
Python
Scala

Approaches to Sort ArrayList of Custom Objects by Property

Following are the two approaches to sort an ArrayList of Custom Objects by Property in Java ?

Functional Approach

This approach organizes operations into distinct methods demonstrating a functional style of programming. The operations are modular, and each method focuses on a single responsibility. The main method acts as the controller, invoking these functions to perform the desired tasks.

Algorithm

Following are the steps to sort an ArrayList of Custom Objects by Property using a functional approach ?

  • Step 1 - START
  • Step 2 - Declare namely
  • Step 3 - Define the values.
  • Step 4 - Use the sort method to sort the list.
  • Step 5 - Use the compareTo method to compare properties of the list.
  • Step 6 - Use the add method to add new values to the list.
  • Step 7 - In the main method, create an array list, and invoke the ?sort' method.
  • Step 8 - Display the result Step 9 - Stop

Example 

Below is an example of Sorting an ArrayList of Custom Objects by Property using a functional approach

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 Demo {
   public static void print(ArrayList<CustomObject> input_list){
      for (CustomObject object : input_list) {
         System.out.println(object.get_custom_property());
      }
   }
   public static void sort(ArrayList<CustomObject> 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<CustomObject> input_list){
      input_list.add(new CustomObject("Java"));
      input_list.add(new CustomObject("Scala"));
      input_list.add(new CustomObject("Python"));
      input_list.add(new CustomObject("Mysql"));
   }
   public static void main(String[] args){
      System.out.println("Required packages have been imported");
      ArrayList<CustomObject> input_list = new ArrayList<>();
      add(input_list);
      System.out.println("The list is defined as ");
      print(input_list);
      sort(input_list);
      System.out.println("\nThe list after sorting values: ");
      print(input_list);
   }
}

Output

Required packages have been imported
The list is defined as
Java
Scala
Python
Mysql

The list after sorting values:
Java
Mysql
Python
Scala

Time Complexity: O(n log n) for sorting.
Space Complexity: O(1), in-place sorting, excluding input size.

Object-Oriented Approach

In this approach, the operations are encapsulated within the main method, adhering to object-oriented programming principles. This style simplifies the flow by reducing external function calls and directly managing the ArrayList within the main method.

Constructor to initialize the property ?

public CustomObject(String property){
      this.custom_property = property;
   }

Getter to access the property ?

public String get_custom_property(){
      return this.custom_property;
}

Sorts the ArrayList of CustomObject in ascending order based on the custom_property using a lambda expression with the compareTo method ?

input_list.sort((object_1, object_2) -> 
   object_1.get_custom_property().compareTo(object_2.get_custom_property()));

Example

Below is an example of Sorting an ArrayList of Custom Objects by Property object-oriented ? programming

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 Demo {
   public static void main(String[] args){
      System.out.println("Required packages have been imported");
      ArrayList<CustomObject> input_list = new ArrayList<>();
      input_list.add(new CustomObject("Java"));
      input_list.add(new CustomObject("Scala"));
      input_list.add(new CustomObject("Python"));
      input_list.add(new CustomObject("Mysql"));
      System.out.println("The number is defined as ");
      for (CustomObject object : input_list) {
         System.out.println(object.get_custom_property());
      }
      input_list.sort((object_1, object_2)
      -> object_1.get_custom_property().compareTo(
      object_2.get_custom_property()));
      System.out.println("\nThe list after sorting values: ");
      for (CustomObject object : input_list) {
         System.out.println(object.get_custom_property());
      }
   }
}

Output

Required packages have been imported
The number is defined as
Java
Scala
Python
Mysql

The list after sorting values:
Java
Mysql
Python
Scala

Time Complexity: O(n log n) for sorting.
Space Complexity: O(1), in-place sorting, excluding input size.

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-01-22T18:37:57+05:30

668 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements