排序

1 、對map排序

 public static<K,V> Map<K,V> sortMapValue(Map<K,V> map,final String...fields){  
        Map<K,V> tempmap=new LinkedHashMap<K,V>();  
        Set<Entry<K,V>> set=new TreeSet<Entry<K,V>>(new Comparator<Entry<K,V>>() {  
            @Override  
            public int compare(Entry<K, V> o1, Entry<K, V> o2) {  
                int valflag=0;  
                final String ClassName=o1.getValue().getClass().getSimpleName();//得到V的類型  
                int keyflag=o1.getKey().toString().compareTo(o2.getKey().toString());  
                //比較基本類型和String類型  
                if(ClassName.equals("String")||ClassName.equals("Byte")||ClassName.equals("Character")||ClassName.equals("Short")||  
                        ClassName.equals("Integer")||ClassName.equals("Long")||ClassName.equals("Float")||ClassName.equals("Double")){  
                    valflag=vCompare(o1.getValue(), ClassName, o2.getValue(), ClassName);  
                    if(valflag!=0){  
                        return valflag;  
                    }else{  
                        return keyflag;  
                    }  
                }else{//比較對象  
                    if(fields!=null&&fields.length<=0){  
                        return 0;  
                    }  
                    Class clazz1=o1.getValue().getClass();  
                    Class clazz2=o2.getValue().getClass();  
                    for(String field:fields){  
                        try {  
                            Field f1=clazz1.getDeclaredField(field);  
                            Field f2=clazz2.getDeclaredField(field);  
                            f1.setAccessible(true);  
                            f2.setAccessible(true);  
                            valflag=vCompare(f1.get(o1.getValue()), f1.getType().getSimpleName(),   
                                    f2.get(o2.getValue()), f2.getType().getSimpleName());  
                            if(valflag!=0){  
                                return valflag;  
                            }else{  
                                return keyflag;//先假設只有一個比較參數  
                            }  
                        } catch (SecurityException e) {  
                            e.printStackTrace();  
                        } catch (NoSuchFieldException e) {  
                            e.printStackTrace();  
                        }catch (IllegalArgumentException e) {  
                            e.printStackTrace();  
                        } catch (IllegalAccessException e) {  
                            e.printStackTrace();  
                        }  
                    }  
                }  
                return valflag;  
            }  
        });  
        for(Map.Entry<K, V> entry:map.entrySet()){  
            set.add(entry);  
        }  
        map.clear();  
        for(Entry<K,V> entry:set){  
//          System.out.println("#"+entry.getKey()+":"+entry.getValue());  
            tempmap.put(entry.getKey(), entry.getValue());  
            map.put(entry.getKey(), entry.getValue());//如果是LinkedHashmap的話就不用在調用的時候賦值了,否則需要重新賦值  
        }  
        return tempmap;  
    }  
/**' 
 *  
 * @param <V>值的類型 
 * @param v1 值1 
 * @param type1 值的 
 * @param v2 
 * @param type2 
 * @return 
 */  
public static<V> int vCompare(V v1,String type1,V v2,String type2){  
    int valflag=0;  
    if(type1.equalsIgnoreCase("String")){  
        return v1.toString().compareTo(v2.toString());  
    }else if(type1.equalsIgnoreCase("Byte")||type1.equalsIgnoreCase("Character")||type1.equalsIgnoreCase("Short")||type1.equalsIgnoreCase("Integer")  
            ||type1.equalsIgnoreCase("Long")||type1.equalsIgnoreCase("Float")||type1.equalsIgnoreCase("Double")||type1.equalsIgnoreCase("int")  
            ||type1.equalsIgnoreCase("char")){  
        valflag=(int)(Double.parseDouble(v1.toString())-Double.parseDouble(v2.toString()));  
//      System.out.println(v1.toString()+":"+v2.toString());  
//      System.out.println(valflag+":"+(Double.parseDouble(v1.toString())+":"+Double.parseDouble(v2.toString())));  
        return valflag;  
    }  
    return 0;  
}  
} 


2

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章