java對象集合的排序

import java.lang.reflect.Field;
import java.text.CollationKey;
import java.text.Collator;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

public class ListComparator<T> implements Comparator<T> {

    private String sortField;// 對象中需要排序的字段名稱,即private type filedName;

    private int fieldType;// 對應字段的類型,暫時只有String 和 Number兩種

    private boolean orderASC = true;// 升序or降序

    public static final int STRING = 1;

    public static final int NUMERIC = 2;

    public ListComparator(String sortField, int fieldType) {
        super();
        this.sortField = sortField;
        this.fieldType = fieldType;
    }

    public ListComparator(String sortField, int fieldType, boolean orderASC) {
        super();
        this.sortField = sortField;
        this.fieldType = fieldType;
        this.orderASC = orderASC;
    }

    /**
     * 由構造器構造需要的字段sortField,fieldType,orderASC
     *
     * @param o1
     *            對象1
     * @param o2
     *            對象2
     * @return 比較結果
     * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
     */
    @SuppressWarnings("unchecked")
    public int compare(Object o1, Object o2) {
        int result = 0;
        try {
            Field filed1 = ((T) o1).getClass().getDeclaredField(sortField);
            Field filed2 = ((T) o2).getClass().getDeclaredField(sortField);
            filed1.setAccessible(true);
            filed2.setAccessible(true);

            if (STRING == fieldType) {
                String value1 = (String) filed1.get(o1);
                String value2 = (String) filed2.get(o2);
                if (value1 == null) {
                    result = -1;
                } else if (value2 == null) {
                    result = 1;
                } else {
                    result = value1.compareTo(value2);
                }
            } else if (NUMERIC == fieldType) {
                Double value1 = (Double) filed1.get(o1);
                Double value2 = (Double) filed2.get(o2);
                if (value1 == null) {
                    result = -1;
                } else if (value2 == null) {
                    result = 1;
                } else {
                    result = value1.compareTo(value2);
                }
            } else {
                throw new Exception(
                        "Campared Field Type Should Be String Or Numeric");
            }

        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (!orderASC) {
            result = -result;
        }
        return result;
    }

    /**
     * 按照中文字符的拼音順序來排序,其實是按照這些漢字的GBK碼順序來排列的,所以部分漢字沒辦法正確排序
     *
     * @param list
     *            需要排列的list
     * @param sortField
     *            按照那個字段來排序
     * @param fieldType
     *            字段類型
     * @param orderASC
     *            升序/降序
     * @return 排序之後的list
     */
    @SuppressWarnings("unchecked")
    public static List sortListByCN(List list, final String sortField,
            final int fieldType, final boolean orderASC) {
        Collections.sort(list, new Comparator() {
            Collator collator = Collator.getInstance(Locale.SIMPLIFIED_CHINESE);

            public int compare(Object o1, Object o2) {
                int result = 0;
                try {
                    Field filed1 = o1.getClass().getDeclaredField(sortField);
                    Field filed2 = o2.getClass().getDeclaredField(sortField);
                    filed1.setAccessible(true);
                    filed2.setAccessible(true);
                    String value1 = (String) filed1.get(o1);
                    String value2 = (String) filed2.get(o2);
                    if (value1 == null) {
                        result = -1;
                    } else if (value2 == null) {
                        result = 1;
                    } else {
                        CollationKey key1 = collator.getCollationKey(value1);
                        CollationKey key2 = collator.getCollationKey(value2);
                        result = key1.compareTo(key2);
                    }

                } catch (SecurityException e) {
                    e.printStackTrace();
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return result;
            }
        });
        return list;
    }

    /**
     * 按照中文字符的拼音順序來排序,其實是按照這些漢字的GBK碼順序來排列的,所以部分漢字沒辦法正確排序
     *
     * @param list
     *            需要排列的list
     * @param sortField
     *            按照那個字段來排序
     * @param fieldType
     *            字段類型
     * @param orderASC
     *            升序/降序
     * @param firstValue
     *            指定某個字符爲第一個顯示的內容
     * @param lastValue
     *            指定某個字符爲最後一個顯示的內容
     * @return 排序之後的list
     */
    @SuppressWarnings("unchecked")
    public static List sortListByCN(List list, final String sortField,
            final int fieldType, final boolean orderASC,
            final String firstValue, final String lastValue) {
        Collections.sort(list, new Comparator() {
            Collator collator = Collator.getInstance(Locale.SIMPLIFIED_CHINESE);

            public int compare(Object o1, Object o2) {
                int result = 0;
                try {
                    Field filed1 = o1.getClass().getDeclaredField(sortField);
                    Field filed2 = o2.getClass().getDeclaredField(sortField);
                    filed1.setAccessible(true);
                    filed2.setAccessible(true);
                    String value1 = (String) filed1.get(o1);
                    String value2 = (String) filed2.get(o2);
                    if (value1 == null) {
                        result = -1;
                    } else if (value2 == null) {
                        result = 1;
                    } else {
                        CollationKey key1 = collator.getCollationKey(value1);
                        CollationKey key2 = collator.getCollationKey(value2);
                        result = key1.compareTo(key2);
                        if (null != firstValue) {
                            if (value1.equals(firstValue)) {
                                result = -1;
                            } else if (value2.equals(firstValue)) {
                                result = 1;
                            }
                        }
                        if (null != lastValue) {
                            if (value1.equals(lastValue)) {
                                result = 1;
                            } else if (value2.equals(lastValue)) {
                                result = -1;
                            }
                        }
                    }
                } catch (SecurityException e) {
                    e.printStackTrace();
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (!orderASC) {
                    result = -result;
                }
                return result;
            }
        });
        return list;
    }

    /**
     * 按照中文字符的拼音順序來排序,其實是按照這些漢字的GBK碼順序來排列的,所以部分漢字沒辦法正確排序,<br/>
     * 這裏提供給簡單的List<String>來比較,不需要利用反射機制
     *
     * @param list
     *            需要排列的list
     * @param orderASC
     *            升序/降序
     * @param firstValue
     *            指定某個字符爲第一個顯示的內容
     * @param lastValue
     *            指定某個字符爲最後一個顯示的內容
     * @return 排序之後的list
     */
    @SuppressWarnings("unchecked")
    public static List sortStringListByCN(List list, final boolean orderASC,
            final String firstValue, final String lastValue) {
        Collections.sort(list, new Comparator() {
            Collator collator = Collator.getInstance(Locale.SIMPLIFIED_CHINESE);

            public int compare(Object o1, Object o2) {
                int result = 0;
                String value1 = (String) o1;
                String value2 = (String) o2;
                if (value1 == null) {
                    result = -1;
                } else if (value2 == null) {
                    result = 1;
                } else {
                    CollationKey key1 = collator.getCollationKey(value1);
                    CollationKey key2 = collator.getCollationKey(value2);
                    result = key1.compareTo(key2);
                    if (null != firstValue) {
                        if (value1.equals(firstValue)) {
                            result = -1;
                        } else if (value2.equals(firstValue)) {
                            result = 1;
                        }
                    }
                    if (null != lastValue) {
                        if (value1.equals(lastValue)) {
                            result = 1;
                        } else if (value2.equals(lastValue)) {
                            result = -1;
                        }
                    }
                }
                if (!orderASC) {
                    result = -result;
                }
                return result;
            }
        });
        return list;
    }
    // 可以擴展Set的排序
   
   
    // 如果想要從某個map中按序取出值,可以把map的key或者value的集合先進行排序,之後按照key的有序集合來獲取map中的內容
}
 

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