多字段 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 ...
随机推荐
- cmd /c和cmd /k 解释,附★CMD命令★ 大全
cmd /c和cmd /k http://leaning.javaeye.com/blog/380810 java的Runtime.getRuntime().exec(commandStr)可以调用执 ...
- 如何向git账号上提交代码
官方说明:https://help.github.com/articles/generating-ssh-keys/ 1,为Github账户设置SSH key 文章地址:http://zuyunfei ...
- linux网络:常用命令(一)
1.ifconfig 可以查看Linux的网卡情况 ifconfig eth0 查看 eth0的信息 给一块网卡设置多个ip地址: ifconfig eth0:0 192.168.1.12 255. ...
- 全屏背景:15个jQuery插件实现全屏背景图像或媒体
动态网站通常利用背景图像或预加载屏幕,以保证所有资源都加载到页面上,在浏览器中充分呈现.现在很多网站都炫耀自己的图像作为背景图像全屏背景,追溯到旧的Flash网站却用自己的方式在HTML资源重布局. ...
- pygal and matplotlib(again)
之前项目有用过pygal做chart图, 写代码很容易,几行代码就很做出一个看上去还不错的chart, 缺点是: 要调的再美观很难, Web上的交互效果较差. 在web上做可视化还是推荐采用Echar ...
- Swift2.1 语法指南——泛型
原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...
- Mac Mini中添加VNC访问
开启Mac Mini上面的VNC. 1) 打开“系统偏好设置”(System Preference),双击打开“共享”(Sharing)项. 2)在左侧将“屏幕共享”(Screen sharing) ...
- Mysql BLOB和TEXT类型
BLOB是一个二进制大对象,可以容纳可变数量的数据.有4种BLOB类型:TINYBLOB.BLOB.MEDIUMBLOB和LONGBLOB.它们只是可容纳值的最大长度不同. A binary la ...
- Redis学习笔记十:独立功能之监视器
通过执行 monitor 命令可以让客户端自己变成一个监视器,实时接收并打印当前处理的命令请求的相关信息. 127.0.0.1:6379> monitor OK 1451752646.83727 ...
- css3 github javascript
欢迎关注我的 github github博客地址 github地址