Java Program to Sort ArrayList of Custom Objects by Property

JavaCampus InterviewServer Side ProgrammingProgramming

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

Array lists are 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 −

Suppose our input is

The list is defined as
Java
Scala
Python
Mysql

The desired output would be

The list after sorting values:
Java
Mysql
Python
Scala

Algorithm

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 1

Here, we bind all the operations together under the ‘main’ function.

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

Example 2

Here, we encapsulate the operations into functions exhibiting 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
raja
Updated on 30-Mar-2022 07:40:55

Advertisements