Comparable Part-2
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.colorrepresents the color of the currentCarobject. Sincecoloris of typeString, it can directly use thecompareTo()method provided by theStringclass.Internally, the
Stringclass implements theComparableinterface and defines its owncompareTo()method. By callingcompareTo(o.color), we pass the color of the otherCarobject to this method.The
String.compareTo()method compares both string values and arranges them in their natural (alphabetical) order, which enables theCarobjects 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
Carobjects to be sorted in descending (reverse alphabetical) order based on color.