import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
*
* Java program to test Object sorting in Java. This Java program
* test Comparable and Comparator implementation provided by Order
* class by sorting list of Order object in ascending and descending order.
* Both in natural order using Comparable and custom Order using Comparator in Java
*
*/
public classObjectSortingExample {
public static voidmain(Stringargs[]) {
//Creating Order object to demonstrate Sorting of Object in Java
Order ord1 = newOrder(101,2000, "Sony");
Order ord2 = newOrder(102,4000, "Hitachi");
Order ord3 = newOrder(103,6000, "Philips");
//putting Objects into Collection to sort
List<Order>orders = new ArrayList<Order>();
orders.add(ord3);
orders.add(ord1);
orders.add(ord2);
//printing unsorted collection
System.out.println("Unsorted Collection : " + orders);
//Sorting Order Object on natural order - ascending
Collections.sort(orders);
//printing sorted collection
System.out.println("List of Order object sorted in natural order : "+ orders);
// Sorting object in descending order in Java
Collections.sort(orders, Collections.reverseOrder());
System.out.println("List of object sorted in descending order : "+ orders);
//Sorting object using Comparator in Java
Collections.sort(orders, newOrder.OrderByAmount());
System.out.println("List of Order object sorted using Comparator - amount : " + orders);
// Comparator sorting Example - Sorting based on customer
Collections.sort(orders, newOrder.OrderByCustomer());
System.out.println("Collection of Orders sorted using Comparator - by customer : " + orders);
}
}
/*
* Order class is a domain object which implements
* Comparable interface to provide sorting on natural order.
* Order also provides copule of custom Comparators to
* sort object based uopn amount and customer
*/
class Order implementsComparable<Order> {
private int orderId;
private int amount;
private Stringcustomer;
/*
* Comparator implementation to Sort Order object based on Amount
*/
public static classOrderByAmount implements Comparator<Order> {
@Override
public int compare(Order o1, Order o2) {
returno1.amount >o2.amount ?1 : (o1.amount < o2.amount ? -1 : 0);
}
}
/*
* Anohter implementation or Comparator interface to sort list of Order object
* based upon customer name.
*/
public static classOrderByCustomer implements Comparator<Order> {
@Override
public int compare(Order o1, Order o2) {
returno1.customer.compareTo(o2.customer);
}
}
public Order(intorderId, int amount, Stringcustomer) {
this.orderId = orderId;
this.amount = amount;
this.customer = customer;
}
public int getAmount(){returnamount; }
public void setAmount(int amount){this.amount = amount;}
public StringgetCustomer() {return customer;}
public void setCustomer(Stringcustomer) {this.customer= customer;}
public int getOrderId(){returnorderId;}
public void setOrderId(int orderId){this.orderId = orderId;}
/*
* Sorting on orderId is natural sorting for Order.
*/
@Override
public int compareTo(Order o) {
return this.orderId > o.orderId ? 1 : (this.orderId < o.orderId ? -1 : 0);
}
/*
* implementing toString method to print orderId of Order
*/
@Override
public StringtoString(){
return String.valueOf(orderId);
}
}
Output
Unsorted Collection : [103, 101, 102]
List of Order object sorted in natural order : [101, 102, 103]
List of object sorted in descending order : [103, 102, 101]
List of Order object sorted using Comparator - amount : [101, 102, 103]
Collection of Orders sorted using Comparator - by customer : [102, 103, 101]