多字段 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>多字段 java对象排序的更多相关文章
- Java 对象排序详解
		很难想象有Java开发人员不曾使用过Collection框架.在Collection框架中,主要使用的类是来自List接口中的ArrayList,以及来自Set接口的HashSet.TreeSet,我 ... 
- Java对象排序
		java实现对象比较,可以实现java.lang.Comparable或java.util.Comparator接口 //Product.java import java.util.Date; //p ... 
- java对象排序(Comparable)详细实例
		对象实现Comparable接口 public class Field implements Comparable<Field>{ private String name; private ... 
- [Java] 对象排序示例
		package test.collections; import java.util.ArrayList; import java.util.Collection; import java.util. ... 
- Java对象排序两种方法
		转载:https://blog.csdn.net/wangtaocsdn/article/details/71500500 有时候需要对对象列表或数组进行排序,下面提供两种简单方式: 方法一:将要排序 ... 
- Java笔记12:Java对象排序
		代码: import java.util.Arrays; import java.util.Comparator; class Person { private String name; privat ... 
- Java - 简单的对象排序 - Comparator
		注:对象排序,就是对对象中的某一字段进行比较,以正序或倒序进行排序. 例: 需要排序的对象: public class Person { public int age; public String n ... 
- Java对象比较器对泛型List进行排序-Demo
		针对形如:字段1 字段2 字段3 字段n 1 hello 26 7891 world 89 5562 what 55 4562 the 85 452 fuck 55 995 haha 98 455 以 ... 
- [个人原创]关于java中对象排序的一些探讨(三)
		这篇文章由十八子将原创,转载请注明,并标明博客地址:http://www.cnblogs.com/shibazijiang/ 对对象排序也可以使用Guava中的Ordering类. 构造Orderin ... 
随机推荐
- verilog描述表决器的两种方式简易分析
			命题:设计一个三变量表决器.真值表如下: 可以写出并简化得出公式:F=AB+BC+AC. 以下是两种算法: 第一种:仅从算法方面描述为:A.B.C的和大于1则输出结果为1,否则为0:源码如下: mod ... 
- ServletContext中常用方法
			..获取Tomcat的Context的初始化参数. 1.获取Tomcat的server.xml中设置Context的初始化参数. 例如: <Context path="/testcon ... 
- Apache ab参数--压力测试
			Apache附带的ab,它非常容易使用,ab可以直接在Web服务器本地发起测试请求.这至关重要,因为我们希望测试的服务器的处理时间,而不包含数据的网络传输时间以及用户PC本地的计算时间. 需要清楚的是 ... 
- Linux服务器管理: 系统的进程管理后台进程的切换和相关命令
			1.把进程放入到后台: [root@localhost/]#tar -zcf etc.tar.gz /etc & //这种方法是在后台运行的 [root@localhost ... 
- 如何获取客户端MAC地址(三个方法)
			方法一: 调用Windows的DOS命令,从输出结果中读取MAC地址: public static String getMACAddress() { String address = "&q ... 
- JavaScript数组属性与方法
			Array 对象属性 属性 描述 constructor 返回对创建此对象的数组函数的引用. length 设置或返回数组中元素的数目. prototype 使您有能力向对象添加属性和方法. Arra ... 
- R中的name命名系列函数总结
			本文原创,转载请注明出处,本人Q1273314690 R中关于给行列赋名称的函数有 dimnames,names,rowname,colname,row.names 这五个函数,初学的时候往往分不清楚 ... 
- lustre文件系统部署流程
			# 1 准备工作### 1.1 添加以太网址添加以太网地址,使得gio017可以访问到需要安装的节点.修改gio017上的/etc/hosts,将需要批量操作的节点名以如下方式添加.```[gio01 ... 
- java的封装
- Swift3.0P1 语法指南——字符串与字符
			原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ... 
