1 5 package org.exoplatform.services.communication.sms.util; 6 7 import java.lang.reflect.InvocationTargetException ; 8 import java.util.Comparator ; 9 10 import org.apache.commons.beanutils.PropertyUtils; 11 12 17 public class SortComparator implements Comparator { 18 19 public String p_fieldName; 20 21 public SortComparator(String fieldName) { 22 p_fieldName = fieldName; 23 } 24 25 public int compare(Object o1, Object o2) { 26 try { 27 28 Object val1 = PropertyUtils.getProperty(o1, p_fieldName); 29 Object val2 = PropertyUtils.getProperty(o2, p_fieldName); 30 31 if (val1.equals(val2)) { return 0; } 32 33 if (val1 instanceof String ) { return ((String ) val1).compareTo((String ) val2); } 34 if (val1 instanceof Integer ) { 35 Integer int1 = new Integer ((String ) val1); 36 Integer int2 = new Integer ((String ) val2); 37 38 return int1.compareTo(int2); 39 } 40 if (val1 instanceof java.util.Date ) { 41 java.util.Date date1 = (java.util.Date ) val1; 42 java.util.Date date2 = (java.util.Date ) val2; 43 return date1.compareTo(date2); 44 } 45 46 } catch (IllegalAccessException e) { 47 throw new SortException(e); 48 } catch (InvocationTargetException e) { 49 throw new SortException(e); 50 } catch (NoSuchMethodException e) { 51 throw new SortException(e); 52 } 53 54 return 0; 55 } 56 } 57 58 | Popular Tags |