zoukankan      html  css  js  c++  java
  • 多字段 java对象排序

    public class ReflexUtil {
    static Logger logger = LoggerFactory.getLogger(ReflexUtil.class);

    //getMethod
    static public Object invokeMethod(String propertiesName, Object object) {
    try {
    if(object==null) return null;
    if (!propertiesName.contains(".")) {
    String methodName = "get"+getMethodName(propertiesName);
    Method method = object.getClass().getMethod(methodName);
    return method.invoke(object);
    }
    String methodName = "get"+getMethodName(propertiesName.substring(0,propertiesName.indexOf(".")));
    Method method = object.getClass().getMethod(methodName);
    return invokeMethod(propertiesName.substring(propertiesName.indexOf(".")+1), method.invoke(object));

    } catch (Exception e) {
    logger.error(e.toString(), e);
    return null;
    }
    }

    private static String getMethodName(String fildeName) {
    byte[] items = fildeName.getBytes();
    items[0] = (byte) ((char) items[0] - 'a' + 'A');
    return new String(items);
    }

    public static void main(String args[]) {
    Video video = new Video();
    Album album = new Album();
    album.setAlbumId(346l);
    video.setAlbum(album);
    video.setVideoId(126l);
    System.out.println(ReflexUtil.invokeMethod("album.albumId", video));
    }
    }

    public class CompareUtil {
     
        //sort 1正序 -1 倒序  filed 多字段排序
        public static <t> Comparator createComparator(int sort, String... filed) {
            return new ImComparator(sort, filed);
        }
     
        public static class ImComparator implements Comparator {
            int sort = 1;
            String[] filed;
     
            public ImComparator(int sort, String... filed) {
                this.sort = sort == -1 ? -1 : 1;
                this.filed = filed;
            }
     
            @Override
            public int compare(Object o1, Object o2) {
                int result = 0;
                for (String file : filed) {
                    Object value1 = ReflexUtil.invokeMethod(file, o1);
                    Object value2 = ReflexUtil.invokeMethod(file, o2);
                    if (value1 == null || value2 == null) {
                        continue;
                    }
                    if (!(value1 instanceof Integer) || !(value1 instanceof Integer)) {
                        continue;
                    }
                    int v1 = Integer.valueOf(value1.toString());
                    int v2 = Integer.valueOf(value2.toString());
                    if (v1 == v2) continue;
                    if (sort == 1) {
                        return v1 - v2;
                    } else if (sort == -1) {
                        return v2 - v1;
                    } else {
                        continue;
                    }
                }
                return result;
            }
        }
     
        public static void main(String args[]) {
            LabelAlbum label1 = new LabelAlbum();
            label1.setLabelId(1); label1.setSequnces(1);
            LabelAlbum label2 = new LabelAlbum();
            label2.setLabelId(1);label2.setSequnces(2);
            LabelAlbum label3 = new LabelAlbum();
            label3.setLabelId(3); label3.setSequnces(4);
            LabelAlbum label4 = new LabelAlbum();
            label4.setLabelId(3);label4.setSequnces(3);
            LabelAlbum label5 = new LabelAlbum();
            label5.setLabelId(4);label5.setSequnces(2);
            List<labelalbum> list = new ArrayList<labelalbum>();
            list.add(label1);
            list.add(label2);
            list.add(label3);
            list.add(label4);
            list.add(label5);
            Collections.sort(list, CompareUtil.createComparator(1, "labelId","sequnces"));
            for (int i = 0; i < list.size(); i++) {
                LabelAlbum labelAlbum=list.get(i);
                System.out.println("labelId:"+labelAlbum.getLabelId()+"  sequence:"+labelAlbum.getSequnces());
            }
        }
    }</labelalbum></labelalbum></t>
  • 相关阅读:
    Python基本数据类型
    DNS服务
    在浏览器地址栏输入百度网址之后的故事(面试必考)
    C++对象的赋值和复制
    C++区别于其他语言的知识点总结
    SQL连接查询(最全面)
    源码堆栈信息保存 到此一游
    linux 内核调试之关键函数名记要
    记几个命令 转
    JS 奇淫巧技 转
  • 原文地址:https://www.cnblogs.com/ctaixw/p/5268780.html
Copyright © 2011-2022 走看看