Sunday, September 16, 2012

Java Code : Sorting of ArrayList Objects


// Sorting ArrayList of Objects(Emp) based on Date of Birth
import java.util.*;
import java.text.SimpleDateFormat;
public class SortObjects
{
        public void checkData()
        {
                ArrayList  ar = new ArrayList();
                ar.add(new Emp(5, "ram", "13-Sep-2005 03:00:39 PM"));          
                                                                  // Date is in String format
                ar.add(new Emp(7, "shiva", "27-Oct-2005 09:08:28 AM"));
                ar.add(new Emp(1, "raj", "29-Aug-2005 11:15:07 AM"));
                ar.add(new Emp(4, "amar", "13-Mar-2005 10:29:18 AM"));
                ar.add(new Emp(6, "bhanu", "08-Oct-2005 03:51:34 PM"));
                ar.add(new Emp(11, "ramesh", ""));
                ar.add(new Emp(2, "gopi", null));
                System.out.println("\nBefore Sorting :");
                for(int i=0; i<ar.size(); i++)
                {
                        Emp ob = (Emp) ar.get(i);
                        System.out.println(ob.toString());
                }
                Collections.sort(ar);
                System.out.println("\nAfter Sorting :");
                for(int i=0; i<ar.size(); i++)
                {
                        Emp ob = (Emp) ar.get(i);
                        System.out.println(ob.toString());
                }
        }
        public static void main(String args[])
        {
                SortObjects ob = new SortObjects();
                ob.checkData();
        }
}
class Emp implements Comparable 
{
        private int eno;
        private String ename;
        private String dob;
  
        public Emp(int eno, String ename, String dob) 
        {
                this.eno = eno;
                this.ename = ename;
                this.dob = dob;
        }
        public int getEno()  
        
                return eno; 
        }
        public String getEname() 
        
                return ename; 
        }
        public String getDob()   
        
                return dob; 
        }
        public String toString() 
        {
                return eno + "\t" + ename + "\t" + dob;
        }
  
        public int compareTo(Object ob) throws ClassCastException                     
                                                                                 // Order by Date
        {
                Emp temp = (Emp)ob;
                                      // If this doesn't work, ClassCastException is thrown
                SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss a");
                        Date date1 = null, date2 = null;
              
                try     
                {
                        if ((dob!=null)&&(!dob.equalsIgnoreCase(""))) 
                                        date1 = sdf.parse(dob);
                        
                        if ((temp.dob!=null)&&(!temp.dob.equalsIgnoreCase(""))) 
                                        date2 = sdf.parse(temp.dob);
                }
                catch(Exception e)      
                {       e.printStackTrace();    }
                if ((date1!=null)&&(date2!=null) )
                        return date2.compareTo(date1);
                else
                        return 0;
        }
/*      public int compareTo(Object ob) throws ClassCastException                      
                                                                           // Order by Employee No.
        {
                Emp temp = (Emp)ob; 
                int enum = eno  - temp.eno;
                return enum; 
        }
*/
}