Java对象排序

实现Comparator接口-->Collections.sort(List, new ComparaObject());

public class ComparaObject implements Comparator{

    @Override
    public int compare(User o1, User o2) {
         
          String user1_name = StringUtil.toHanyuPinyin(o1.getName());
          String user2_name = StringUtil.toHanyuPinyin(o2.getName());
        
          int b_photo = o1.getIsHavePhoto().compareTo(o2.getIsHavePhoto());
          int b_contact = o1.getIsContact().compareTo(o2.getIsContact());
          int b_name = user1_name.compareTo(user2_name);
          
       
         if (b_photo ==0) { //先按照是否有照片排序
                   
               if(b_contact == 0) {//再按照是否是联系人排
               return b_name;
            }else  {
                return -b_contact;  //最后按照name的拼音升序
            }
        }else {
            return -b_photo;
        }
    }
}

工具类

//调用
  List sortDesList = new ArrayList(); 
 sortDesList.add(new SortDescription("isHavePhoto",false));       //根据isHavePhoto降序
 sortDesList.add(new SortDescription("isContact",false));         //根据isContact降序
 sortDesList.add(new SortDescription("name",true));               //根据name升序

 Collections.sort(users, new ComparaObject(sortDesList));  
//实现
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Comparator;
import java.util.List;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;


class SortDescription {
    
    private String fildName;
    private Boolean isAsc;
    
    
    public SortDescription(String fildName, Boolean isAsc) {
        super();
        this.fildName = fildName;
        this.isAsc = isAsc;
    }


    public String getFildName() {
        return fildName;
    }


    public void setFildName(String fildName) {
        this.fildName = fildName;
    }


    public Boolean getIsAsc() {
        return isAsc;
    }


    public void setIsAsc(Boolean isAsc) {
        this.isAsc = isAsc;
    }

}

public class ComparaObject implements Comparator{

    private List sortDesList;

    public List getSortDesList() {
        return sortDesList;
    }

    public void setSortDesList(List sortDesList) {
        this.sortDesList = sortDesList;
    }

    public ComparaObject(List sortDesList) {
        super();
        this.sortDesList = sortDesList;
    }

    int comparaTwoObj(SortDescription des, Object o1, Object o2) {
        
        String value1 = null;
        String value2 = null;
         try {
             value1 = hendleReturnValue(o1, des.getFildName());
             value2 = hendleReturnValue(o2, des.getFildName());
             
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int compara =  toHanyuPinyin(value1).compareTo(toHanyuPinyin(value2));
        
        return compara;
    }
    
    private String hendleReturnValue(Object obj, String fildName) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        
        String methodName = "get" + fildName.substring(0, 1).toUpperCase() + fildName.substring(1);  
        
         Object obj1 = InvokeWithObj(obj, methodName, null);
         if (obj1 instanceof String) {
             return  (String) obj1;
        }else {
            return  obj1 + "";
        }
    }
    
    @Override
    public int compare(Object o1, Object o2) {
        
    for (int i = 0; i < this.sortDesList.size(); i++) {
        
        SortDescription des = this.sortDesList.get(i);
        
        int compara = comparaTwoObj(des, o1, o2);
        
        if (compara != 0) {
            return des.getIsAsc() == true ? compara : -compara;
        }else {
           continue;    
        }
    }   
    return 0;
  }

    
     //根据对象,方法名,参数调用方法
       private Object InvokeWithObj (Object obj, String methodName, Object ...args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
          
                Class prams[] =  getPramsClass(args);
                Method method = getKindOfClassMethod(obj.getClass(),methodName,prams);
                method.setAccessible(true); //设置可以访问私有方法
                Object returnObj = method.invoke(obj, args);
                return  returnObj;
         } 
       //根据参数对象数组获取参数类数组
       private Class[] getPramsClass ( Object ...args){
            
            if (args == null) {
                return null;
            }
            
             Class[] prams = new Class[args.length];
                for (int i = 0; i < args.length; i++) {
                    prams[i] = args[i].getClass();
                }
                return prams;
        } 
        //获取类的指定方法,子类没有向父类找 
         private Method getKindOfClassMethod(Class cls, String methodName, Class[] prams) {
                
               Method method = null;
               Class clazz = cls;
               while (method == null && clazz != Object.class) {
                  try {
                     method = clazz.getDeclaredMethod(methodName, prams);   
                  } catch (NoSuchMethodException e) {
                     e.printStackTrace();
                     clazz = clazz.getSuperclass();
                 } 
           }
             return method;
     } 
         private String toHanyuPinyin(String ChineseLanguage){
             if (ChineseLanguage == null) {
                return "";
            }
                char[] cl_chars = ChineseLanguage.trim().toCharArray();
                String hanyupinyin = "";
                HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
                defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 输出拼音全部小写
                defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调
                defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V) ;
                try {
                    for (int i=0; i 
 

利用快速排序实现

public class JMQuickSort {

    private static List sortDesList;
    
    public List getSortDesList() {
        return sortDesList;
    }
    public void setSortDesList(List sortDesList) {
        JMQuickSort.sortDesList = sortDesList;
    }
    
    public static  void quickSort(List sortDesList, List objects) {
        
        JMQuickSort.sortDesList = sortDesList;
        
        sort(objects, 0, objects.size()-1);
    }
    //快速排序
    private static  void sort(List objects, int left, int right){
            
            if (left >= right) {  return; }
            
            int i = left, j = right;
            T obj = objects.get(left);
            
            while(i < j) {
                
                while(i < j && objectCompara(obj, objects.get(j)) <= 0) {
                    j--;
                }
                T usj = objects.get(j);
                objects.remove(i);
                objects.add(i, usj);
                
                while(i < j && objectCompara(obj, objects.get(i)) >= 0) {
                    i++;
                }
                T usi = objects.get(i);
                objects.remove(j);
                objects.add(j, usi);
            }
            objects.remove(i);
            objects.add(i, obj);
            
            sort(objects, left, i-1);
            sort(objects, i+1, right);
        }
        
        static int objectCompara(Object o1, Object o2) {
            //大于0--->o1大
            for (int i = 0; i < JMQuickSort.sortDesList.size(); i++) {
                
                JMSortDes des = JMQuickSort.sortDesList.get(i);
                
                int compara = comparaTwoObj(des, o1, o2);
                
                if (compara != 0) {
                    return des.getIsAsc() == true ? compara : -compara;
                }else {
                   continue;    
                }
            }
            return 0;
     }
        private static int comparaTwoObj(JMSortDes des, Object o1, Object o2) {
            
            String value1 = null;
            String value2 = null;
             try {
                 value1 = hendleReturnValue(o1, des.getFildName());
                 value2 = hendleReturnValue(o2, des.getFildName());
                 
            } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            int compara =  toHanyuPinyin(value1).compareTo(toHanyuPinyin(value2));
            
            return compara;
        }
        private static String hendleReturnValue(Object obj, String fildName) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
            
            String methodName = "get" + fildName.substring(0, 1).toUpperCase() + fildName.substring(1);  
            
             Object obj1 = InvokeWithObj(obj, methodName, null);
             if (obj1 instanceof String) {
                 return  (String) obj1;
            }else {
                return  obj1 + "";
            }
        }
         private static String toHanyuPinyin(String ChineseLanguage){
             if (ChineseLanguage == null) {
                return "";
            }
                char[] cl_chars = ChineseLanguage.trim().toCharArray();
                String hanyupinyin = "";
                HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
                defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 输出拼音全部小写
                defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调
                defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V) ;
                try {
                    for (int i=0; i

你可能感兴趣的:(Java对象排序)