Java Program to Sort ArrayList of Custom Objects By Property

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.

Let’s look at a Java Program to Sort ArrayList of Custom Objects By Property

Example 1: Sort ArrayList of Custom Objects By Property

Run
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

Example 2 :Java Program to sort Array List of custom object by property.

Run
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

Run
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(ArrayList input_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

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

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription