java List集合根据某一个字段排序

public class SortListUtil {
    public static final String ASC = "asc";
    public static final String DESC = "desc";

    public static void main(String[] args) {
        List listOfAnimals = Arrays.asList(
                new Animal().setName("猫咪").setAge(1),
                new Animal().setName("鸭子").setAge(3),
                new Animal().setName("老鼠").setAge(2)
        );
        // 升序
        SortListUtil.sort(listOfAnimals, new String[]{"age"}, new String[]{SortListUtil.ASC});
        listOfAnimals.forEach(System.out::println);
    }

    /**
     * List集合根据某一个字段排序
     *
     * @param list
     * @param fields
     * @param sorts
     * @return
     */
    @SuppressWarnings("unchecked")
    public static List sort(List list, String[] fields, String[] sorts) {
        if (fields != null && fields.length > 0) {
            for (int i = fields.length - 1; i >= 0; i--) {
                final String field = fields[i];
                String tmpSort = ASC;
                if (sorts != null && sorts.length > i && sorts[i] != null) {
                    tmpSort = sorts[i];
                }

                final String sort = tmpSort;
                System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");
                try {
                    Collections.sort(list, new Comparator() {
                        public int compare(Object a, Object b) {
                            int ret = 0;
                            try {
                                //Field f = a.getClass().getDeclaredField(field);
                                Method m = a.getClass().getMethod("get" + captureName(field));
                                Object type = m.invoke(a);
                                // f.setAccessible(true);
                                //Class type = f.getType();

                                if (null == m.invoke(a) || null == m.invoke(b)) {
                                    return ret;
                                }
                                if (type.getClass() == int.class || type.getClass() == Integer.class) {
                                    ret = ((Integer) m.invoke(a)).compareTo(((Integer) m.invoke(b)));
                                } else if (type == double.class) {
                                    ret = ((Double) m.invoke(a))
                                            .compareTo((Double) m.invoke(b));
                                } else if (type == long.class) {
                                    ret = ((Long) m.invoke(a)).compareTo((Long) m.invoke(b));
                                } else if (type == float.class) {
                                    ret = ((Float) m.invoke(a))
                                            .compareTo((Float) m.invoke(b));
                                } else if (type == Date.class) {
                                    ret = ((Date) m.invoke(a)).compareTo((Date) m.invoke(b));
                                } else {
                                    ret = String.valueOf(m.invoke(a)).compareTo(
                                            String.valueOf(m.invoke(b)));
                                }

                            } catch (Exception e) {
                                e.printStackTrace(); //可以报错 但是排序 还是 返回 零
                                return 0;
                            }

                            if (sort != null && sort.toLowerCase().equals(DESC)) {
                                return -ret;
                            } else {
                                return ret;
                            }
                        }
                    });
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return list;
    }

    public static String captureName(String name) {
        char[] cs = name.toCharArray();
        cs[0] -= 32;
        return String.valueOf(cs);
    }
}

运行:

java List集合根据某一个字段排序_第1张图片

 

 

你可能感兴趣的:(笔记,java,linq,webview)