// 9. Write a generic program to sort numerical array and alphabetical array.
// Importing generic java files
import java.util.*;
// Implements comparable interface into custom class
class Car implements Comparable {
int ModalNo;
String name;
int stock;
// Parameterized constructor of the class
Car(int ModalNo, String name, int stock)
{
this.ModalNo = ModalNo;
this.name = name;
this.stock = stock;
}
// Override the compareTo method
public int compareTo(Car car)
{
if (stock == car.stock)
return 0;
else if (stock > car.stock)
return 1;
else
return -1;
}
}
// Main driver method
class Program9B {
// Main driver method
public static void main(String[] args)
{
// Create the ArrayList object
ArrayList c = new ArrayList();
c.add(new Car(2018, "Kia", 20));
c.add(new Car(2020, "MG", 13));
c.add(new Car(2013, "creta", 10));
c.add(new Car(2015, "BMW", 50));
c.add(new Car(2017, "Audi", 45));
// Call the sort function
Collections.sort(c);
// Iterate over ArrayList using for each loop
for (Car car : c) {
// Print the sorted ArrayList
System.out.println(car.ModalNo + " " + car.name
+ " " + car.stock);
}
}
}