What is sorting ?
Putting collection of data in some order is called sorting.
Luckily Java provides a Collections.sort method to sort a set of arraylist objects .Lets take a small example for sorting a collection of string objects.
package app;
import java.util.ArrayList;
import java.util.Collections;
public class SortingList
{
/**
* @param args
*/
public static void main(String[] args)
{
System.out.println("Sorting String Objects");
ArrayList
Emp_list.add("Uday");
Emp_list.add("Mani");
Emp_list.add("Raja");
Emp_list.add("Yuva");
Emp_list.add("Abi");
Emp_list.add("Brindha");
System.out.println(Emp_list);
Collections.sort(Emp_list);
System.out.println(Emp_list);
}
}
Output :
Sorting String Objects
[Uday, Mani, Raja, Yuva, Abi, Brindha]
[Abi, Brindha, Mani, Raja, Uday, Yuva]
If you pass some objects other than string it will throw classcastException.
Why ?
String class implements comparable interface and overridden the compare method
thats why we can sort an array of String objects.if you want sort an object other than string
you should implement either comparator or comparable intefaces .
See the below program for sorting a Customer Objects
package app;
import java.util.*;
class Customer
{
int Cust_ID;
String Cust_Name;
String Ac_type;
int Balance_amt;
Customer()
{
}
Customer(int Id,
String Name,
String Type,
int amt)
{
this.Cust_ID = Id;
this.Cust_Name = Name;
this.Ac_type = Type;
this.Balance_amt = amt;
}
public Integer getId()
{
return Cust_ID;
}
public String getName()
{
return Cust_Name;
}
public Integer getBalance()
{
return Balance_amt;
}
@Override
public String toString()
{
// TODO Auto-generated method stub
return "EMPID :" + Cust_ID + "EMPNAME :" + Cust_Name + "A/C Type :"
+ Ac_type + "AMT :" + Balance_amt + "\n";
}
}
class Bank
{
final static Comparator
public int compare(Customer cust1, Customer cust2) {
return cust1.getId().compareTo(cust2.getId());
}
};
final static Comparator
public int compare(Customer cust1, Customer cust2) {
return cust1.getName().compareTo(cust2.getName());
}
};
final static Comparator
public int compare(Customer cust1, Customer cust2) {
return cust1.getBalance().compareTo(cust2.getBalance());
}
};
public static void main(String st[])
{
ArrayList
Emp_List.add(new Customer(118, "Raja", "Saving", 5000));
Emp_List.add(new Customer(136, "mani", "Saving", 8000));
Emp_List.add(new Customer(173, "Selva", "Saving", 15000));
Emp_List.add(new Customer(108, "kanna", "Saving", 56000));
Emp_List.add(new Customer(18, "Abi", "Saving", 12000));
System.out.println("Original List of Customer Objects");
System.out.println(Emp_List);
System.out.println("Original Customer Objects by Emp ID");
Collections.sort(Emp_List, EMP_ID);
System.out.println(Emp_List);
System.out.println("Original Customer Objects by Amount");
Collections.sort(Emp_List, EMP_AMOUNT);
System.out.println(Emp_List);
}
}
Output :
Original List of Customer Objects
[EMPID :118EMPNAME :RajaA/C Type :SavingAMT :5000
, EMPID :136EMPNAME :maniA/C Type :SavingAMT :8000
, EMPID :173EMPNAME :SelvaA/C Type :SavingAMT :15000
, EMPID :108EMPNAME :kannaA/C Type :SavingAMT :56000
, EMPID :18EMPNAME :AbiA/C Type :SavingAMT :12000
]
Original Customer Objects by Emp ID
[EMPID :18EMPNAME :AbiA/C Type :SavingAMT :12000
, EMPID :108EMPNAME :kannaA/C Type :SavingAMT :56000
, EMPID :118EMPNAME :RajaA/C Type :SavingAMT :5000
, EMPID :136EMPNAME :maniA/C Type :SavingAMT :8000
, EMPID :173EMPNAME :SelvaA/C Type :SavingAMT :15000
]
Original Customer Objects by Amount
[EMPID :118EMPNAME :RajaA/C Type :SavingAMT :5000
, EMPID :136EMPNAME :maniA/C Type :SavingAMT :8000
, EMPID :18EMPNAME :AbiA/C Type :SavingAMT :12000
, EMPID :173EMPNAME :SelvaA/C Type :SavingAMT :15000
, EMPID :108EMPNAME :kannaA/C Type :SavingAMT :56000
]
