Java 反射类:ReflexUtil

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));
}
}

Java 对象排序 : CompareUtil ()

package com.gochinatv.vrs.framework.util;

import com.alibaba.fastjson.JSONObject;
import com.gochinatv.vrs.framework.bean.LabelAlbum;
import com.gochinatv.vrs.framework.bean.Video; import java.util.*; /**
 * Created by shhao
 * Date: 14-5-6.
 * Time:下午4:48
 */
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) { //Integer整数序排序
                    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 if (value1 instanceof Date) {  //Date类型排序
                    Date d1 = (Date) value1;
                    Date d2 = (Date) value2;
                    if (d1.compareTo(d2) == 0) continue;
                    if (sort == 1) {
                        return d1.compareTo(d2);
                    } else if (sort == -1) {
                        return d2.compareTo(d1);
                    }
                } else if (value1 instanceof Long) { //Long排序
                    long v1 = Long.valueOf(value1.toString());
                    long v2 = Long.valueOf(value2.toString());
                    if (v1 == v2) continue;
                    if (sort == 1) {
                        return v1 > v2 ? 1 : -1;
                    } else if (sort == -1) {
                        return v2 > v1 ? 1 : -1;
                    }
                } else if (value1 instanceof Double) { //Double排序
                    Double v1 = Double.valueOf(value1.toString());
                    Double v2 = Double.valueOf(value2.toString());
                    if (v1 == v2) continue;
                    if (sort == 1) {
                        return v1 > v2 ? 1 : -1;
                    } else if (sort == -1) {
                        return v2 > v1 ? 1 : -1;
                    }
                }             }             return result;
        }
    }     public static void main(String args[]) {
        Video label1 = new Video();
        label1.setDisplayOrder(1);
        label1.setCreateTime(DateUtils.convert("2014-01-01"));
        Video label2 = new Video();
        label2.setDisplayOrder(3);
        label2.setCreateTime(DateUtils.convert("2014-01-02"));
        Video label3 = new Video();
        label3.setDisplayOrder(2);
        label3.setCreateTime(DateUtils.convert("2014-01-03"));
        Video label4 = new Video();
        label4.setDisplayOrder(2);
        label4.setCreateTime(DateUtils.convert("2014-01-01"));
        Video label5 = new Video();
        label5.setDisplayOrder(4);
        label5.setCreateTime(DateUtils.convert("2014-01-05"));
        List<Video> list = new ArrayList<Video>();
        list.add(label1);
        list.add(label2);
        list.add(label3);
        list.add(label4);
        list.add(label5);
        Collections.sort(list, CompareUtil.createComparator(1, "createTime", "displayOrder"));
        for (int i = 0; i < list.size(); i++) {
            Video labelAlbum = list.get(i);
            System.out.println("displayorder:" + labelAlbum.getDisplayOrder() + "  sequence:" + DateUtils.convert(labelAlbum.getCreateTime()));
        }
    }
}

Java 多字段排序Comparator(兼容Date,Integer,Doubel,Long)的更多相关文章

  1. Java 8 Lambda排序 : Comparator example

    1. Classic Comparator example. Comparator<Developer> byName = new Comparator<Developer>( ...

  2. Comparable与Comparator,java中的排序与比较

    1:比较和排序的概念 比较:两个实体类之间按>,=,<进行比较. 排序:在集合类中,对集合类中的实体进行排序.排序基于的算法基于实体类提供的比较函数. 基本型别都提供了默认的比较算法,如s ...

  3. Vector 多字段排序的Java实现

    要求实现: Vector 多字段排序,其中首元素不参与排序,第一二三字段升序,空排到前面. //这里是Vector的元素定义 public class AVectorElement { private ...

  4. java的list集合如何根据对象中的某个字段排序?

    转自:http://blog.csdn.net/wangjuan_01/article/details/51351633 List集合按某个字段排序 package wjtest_01; import ...

  5. java实现按对象某个字段排序,排序字段和规则自定义

    @SuppressWarnings({ "unchecked", "rawtypes" }) private <T> void sort(List& ...

  6. JAVA实现按列表中元素的时间字段排序

    JAVA代码实现按列表中元素的时间字段排序 导语: 工作中遇到一个问题,调用第三方接口返回的数据没有按时间倒序排列,测试说要加,然后在网上找到一个解决办法,这里记录一下 需求: 如下图列表,按生日进行 ...

  7. Java TreeSet集合 比较器排序Comparator的使用

    比较器排序Comparator的使用 存储学生对象,并遍历,创建TreeSet集合使用带参构造方法 要求,按照学生年龄从小到大排序,如果年龄相同,则按照姓名的字母循序排序 结论 用TreeSet集合存 ...

  8. 在Java中使用Collections.sort 依据多个字段排序

    一.如何使用Collections工具类进行排序 使用Collections工具类进行排序主要有两种方式: 1.对象实现Comparable接口,重写compareTo方法 /** * @author ...

  9. ElasticSearch java API 按照某个字段排序

    searchRequestBuilder.addSort("publish_time", SortOrder.DESC); 按照某个字段排序的话,hit.getScore()将会失 ...

随机推荐

  1. hdu 2104

    #include <iostream> using namespace std; int gcd(int a,int b) { return (b?gcd(b,a%b):a); } int ...

  2. 国内使用google地图的初级使用

    <!DOCTYPE html><html><head><title>Simple Map</title><meta name=&quo ...

  3. (转)解析php中die(),exit(),return的区别

    本篇文章是对php中die(),exit(),return的区别进行了详细的分析介绍,需要的朋友参考下     die()停止程序运行,输出内容exit是停止程序运行,不输出内容return是返回值d ...

  4. DeviceToken 获取失败,原因:Error Domain=NSCocoaErrorDomain Code=3000 "未找到应用程序的“aps-environment”的授权字符串"...

    apns -> 注册推送功能时发生错误, 错误信息: Error Domain=NSCocoaErrorDomain Code=3000 "未找到应用程序的“aps-environme ...

  5. mysql UNIX时间戳与日期的相互转换

    UNIX时间戳转换为日期用函数: FROM_UNIXTIME() select FROM_UNIXTIME(1156219870); 日期转换为UNIX时间戳用函数: UNIX_TIMESTAMP() ...

  6. 武汉科技大学ACM:1010: 零起点学算法89——母牛的故事

    Problem Description 有一头母牛,它每年年初生一头小母牛.每头小母牛从第四个年头开始,每年年初也生一头小母牛.请编程实现在第n年的时候,共有多少头母牛? Input 输入数据由多个测 ...

  7. IO流(文件字节输入输出

    输入输出流可能有不允许操作,可能有出现错误,必须在try语句中进行 FileOutputStream out1=new FileOutputStream("test1.txt") ...

  8. IO流(File类

    File类 三个构造方法 File(String filename)//模式和应用程序一个目录下 File(String directoryPath,String filename)//文件的绝对路径 ...

  9. java2实用教程102小程序(分数计算和流水线计算

    import java.util.Scanner; public class test{ public static void main(String args[]){ Rational a=new ...

  10. mysql命令行执行外部文件

    mysql命令行执行外部文件