多字段 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 ...
随机推荐
- gradle 的sourceCompatibility 与 targetCompatibility 区别
sourceCompatibility:指定编译编译.java文件的jdk版本 targetCompatibility:确保class文件与targetCompatibility指定版本,或者更新的j ...
- commons-pool实战之 GenericObjectPool和GenericKeyedObjectPool
前面两篇文章说了怎么样简单的使用commons-pool库,这里需要考虑一个问题就是在很多时候我们在池里的对象都是比较重型的并且大多数比较稀缺的 资源,比如说数据库连接,这样如果一直把一些连接放在池里 ...
- XPath使用小结
参考资料: http://www.w3school.com.cn/xpath/xpath_nodes.asp
- motto2
Baby you've done enough that cut your breath.Don't beat yourself up don't need to turn so fast.Somet ...
- mapreduce 自定义数据类型的简单的应用
本文以手机流量统计为例: 日志中包含下面字段 现在需要统计手机的上行数据包,下行数据包,上行总流量,下行总流量. 分析:可以以手机号为key 以上4个字段为value传传递数据. 这样则需要自己定义一 ...
- java.lang.reflect.Field
java.lang.reflect.Field 一.Field类是什么 Field是一个类,位于java.lang.reflect包下. 在Java反射中 Field类描述的是 类的属性信息,通俗来讲 ...
- 使用python标准库urllib2访问网页
#访问不需要登录的网页import urllib2target_page_url='http://10.224.110.118/myweb/view.jsp' f = urllib2.urlopen( ...
- C#集合类图继承关系一览表
- [Linux] Chang DNS Setting on Linux
主机的虚拟机使用 NAT 模式时, NAT的DNS不好用.于是需要将虚拟机的DNS改成和主机的一样,这样虚拟机也可以请求互联网资源. Linux的DNS 服务器定义在 /etc/resolv.conf
- bootstrap实现弹出窗口
bootstrap使用modal-dialog实现弹对话框. 一个对话框包含3部分: 对话框头部 modal-header 对话框内容体 modal-body 对话框底部 modal-footer 如 ...