Skip to main content

Command Palette

Search for a command to run...

Comparable Part-2

Updated
3 min read

Earlier, we learned how to sort the Car class by mileAge.
In this article, we will focus on sorting cars by their color, helping us understand how Comparable works with String values.

If you haven’t read it yet, you can check it out here:
👉 Sorting Car Objects by Mileage Using Comparable in Java

Sorting Car Objects By Color:

public class Car implements Comparable<Car>{
    String brand;
    String color;
    int mileAge;
    int numOfSeaters;

    public Car(String brand, String color, int mileAge, int numOfSeaters) {
        this.brand = brand;
        this.color = color;
        this.mileAge = mileAge;
        this.numOfSeaters = numOfSeaters;
    }

    @Override
    public int compareTo(Car o) {
        return this.color.compareTo(o.color); //ascending order
    }


    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", Color='" + color + '\'' +
                ", mileAge=" + mileAge +
                ", numOfSeaters=" + numOfSeaters +
                '}';
    }
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class SortWithColor {
    public static void main(String[] args) {
        List<Car> cars= new ArrayList<Car>();
        cars.add(new Car("BMW", "RED",100,5));
        cars.add(new Car("MARUTI", "BLUE",80,3));
        cars.add(new Car("AUDI", "GREEN",60,7));
        cars.add(new Car("Toyota", "WHITE",120,4));
        Collections.sort(cars);
        cars.forEach(System.out::println);
    }
}

this.color.compareTo(o.color)

Here, this.color represents the color of the current Car object. Since color is of type String, it can directly use the compareTo() method provided by the String class.

Internally, the String class implements the Comparable interface and defines its own compareTo() method. By calling compareTo(o.color), we pass the color of the other Car object to this method.

The String.compareTo() method compares both string values and arranges them in their natural (alphabetical) order, which enables the Car objects to be sorted based on color.

Output :

Car{brand='MARUTI', Color='BLUE', mileAge=80, numOfSeaters=3}
Car{brand='AUDI', Color='GREEN', mileAge=60, numOfSeaters=7}
Car{brand='BMW', Color='RED', mileAge=100, numOfSeaters=5}
Car{brand='Toyota', Color='WHITE', mileAge=120, numOfSeaters=4}

Sorting Car Objects By Color (Descending order):

public class Car implements Comparable<Car>{
    String brand;
    String color;
    int mileAge;
    int numOfSeaters;

    public Car(String brand, String color, int mileAge, int numOfSeaters) {
        this.brand = brand;
        this.color = color;
        this.mileAge = mileAge;
        this.numOfSeaters = numOfSeaters;
    }

    @Override
    public int compareTo(Car o) {
        return o.color.compareTo(this.color); //descending order
    }


    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", Color='" + color + '\'' +
                ", mileAge=" + mileAge +
                ", numOfSeaters=" + numOfSeaters +
                '}';
    }
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class SortWithColor {
    public static void main(String[] args) {
        List<Car> cars= new ArrayList<Car>();
        cars.add(new Car("BMW", "RED",100,5));
        cars.add(new Car("MARUTI", "BLUE",80,3));
        cars.add(new Car("AUDI", "GREEN",60,7));
        cars.add(new Car("Toyota", "WHITE",120,4));
        Collections.sort(cars);
        cars.forEach(System.out::println);
    }
}

Output :

Car{brand='Toyota', Color='WHITE', mileAge=120, numOfSeaters=4}
Car{brand='BMW', Color='RED', mileAge=100, numOfSeaters=5}
Car{brand='AUDI', Color='GREEN', mileAge=60, numOfSeaters=7}
Car{brand='MARUTI', Color='BLUE', mileAge=80, numOfSeaters=3}

o.color.CompareTo(this.color)

In this case, the order of comparison is reversed. Instead of comparing the current car’s color with the other car’s color, we compare the other car’s color with the current car’s color.

Because of this reversal, the result of the comparison is also reversed, which causes the Car objects to be sorted in descending (reverse alphabetical) order based on color.

Sorting

Part 3 of 3

Sorting helps us arrange data in a particular order, like ascending or descending, which makes searching and analysing data easier. In Java, sorting can be done in multiple ways: using arrays, lists, or collections.

Start from the beginning

Basic Sorting

Sorting Basics in Java : A Simple Example Sorting is one of the fundamental concepts in programming. It helps us arrange data in a particular order, like ascending or descending, which makes searching and analysing data easier. In Java, sorting can b...