---
title: "compare 应用实例"
author: "luyige007"
date: "2011-11-14T17:01:54Z"
tags: []
categories: ["编程语言"]
slug: "compare"
draft: false
lastmod: "2011-11-14T17:01:54Z"
id: 1258455
url: "https://luyige007.iteye.com/blog/1258455"
---

整理2个compare 接口排序的实现方法：

 

 

一、 自己实现比较方法，包括一些复杂的多条件比较

 

Java代码  [![收藏代码](http://tree-161219.iteye.com/images/icon_star.png)](http://tree-161219.iteye.com/blog/1124656 "收藏这段代码")

01. public class Comparator {
02. private String mothdName;
03. /\**
04. * 比较器，可以根据具体VO定制排序
05. * @param mothdName
06. * @return
07. \*/
08. public java.util.Comparator getComparator(final String mothdName) {
09. this.mothdName = mothdName;
10. return new java.util.Comparator() {
11. public int compare(Object o1, Object o2) {
12. if (o1 instanceof String) {
13. return compare((String) o1, (String) o2);
14. } else if (o1 instanceof Integer) {
15. return compare((Integer) o1, (Integer) o2);
16. }else if (o1 instanceof Timestamp) {
17. return compare((Integer) o1, (Integer) o2);
18. }else if (o1 instanceof Date) {
19. return compare((Integer) o1, (Integer) o2);
20. } else if (o1 instanceof BpmTaskVo) {
21. return compare((BpmTaskVo) o1, (BpmTaskVo) o2);
22. } else {
23. System.err.println("未找到合适的比较器");
24. return 1;
25. }
26. }
27. public int compare(String o1, String o2) {
28. return o1.compareTo(o2);
29. }
30. public int compare(Integer o1, Integer o2) {
31. return o1.compareTo(o2);
32. }
33. public int compare(Boolean o1, Boolean o2) {
34. return o1.compareTo(o2);
35. }
36. public int compare(Timestamp o1, Timestamp o2){
37. return o1.compareTo(o2);
38. }
39. public int compare(Date o1, Date o2){
40. return o1.compareTo(o2);
41. }
42. //为BpmTaskVo 定制排序
43. public int compare(BpmTaskVo vo1, BpmTaskVo vo2) {
44. Class&lt;BpmTaskVo&gt; voClass = BpmTaskVo.class;
45. Method mothdGet;
46. // mGet.setAccessible(true);//因为写成private 取值可以不设置
47. int i = 0;
48. try {
49. mothdGet = voClass.getDeclaredMethod(mothdName);
50. Object o1 = mothdGet.invoke(vo1);
51. Object o2 = mothdGet.invoke(vo2);
52. //                  System.out.println("方法名"+mothdName + "比较值o1 = "+o1 + "    o2 = "+o2);
53. i = compare(o1,o2);
54. } catch (Exception e) {
55. e.printStackTrace();
56. }
57. return i;
58. }
59. // 多条件比较
60. public int compare(BpmTaskVo o1, BpmTaskVo o2) {
61. String firstname1 = o1.getFirstName();
62. String firstname2 = o2.getFirstName();
63. String lastname1 = o1.getLastName();
64. String lastname2 = o2.getLastName();
65. Boolean sex1 = o1.getSex();
66. Boolean sex2 = o2.getSex();
67. Integer age1 = o1.getAge();
68. Integer age2 = o2.getAge();
69. return (compare(firstname1, firstname2) == 0 ? (compare(lastname1, lastname2) == 0 ? (compare(sex1, sex2) == 0 ? (compare(age1, age2) == 0 ? 0 : compare(age1, age2)) : compare(sex1, sex2)) : compare(lastname1, lastname2)) : compare(firstname1, firstname2));
70. }
71. };
72. }
73. }

 

 

二、 利用反射和compareTo，动态比较。这种是比较好的方法

 

Java代码  [![收藏代码](http://tree-161219.iteye.com/images/icon_star.png)](http://tree-161219.iteye.com/blog/1124656 "收藏这段代码")

01. public class RmComparator {
02. /\**
03. * 比较器，可以根据VO具体字段比较
04. \*
05. * @param mothdName
06. * @return
07. \*/
08. public Comparator getComparator(final String mothdName) {
09. return new Comparator() {
10. public int compare(Object o1, Object o2) {
11. int val = 0;//默认不排序
12. try {
13. if (mothdName == null) {
14. val = myCompare(o1, o2);
15. } else {
16. val = myCompare(getValue(o1, mothdName), getValue(o2, mothdName));
17. }
18. } catch (Exception ex) {
19. ex.printStackTrace();
20. }
21. return val;
22. }
23. // VO中取值
24. public Object getValue(Object bean, String methodName) throws Exception {
25. Method getMethod = bean.getClass().getMethod(methodName, null);
26. return getMethod.invoke(bean, null);
27. }
28. // 具体执行比较的方法
29. public int myCompare(Object obj1, Object obj2) throws Exception {
30. if (obj1 == null || obj2 == null){
31. return obj1 == null ? -1 : 1;
32. }
33. Class cl = obj1.getClass();
34. if (obj1 instanceof java.lang.Comparable) {
35. // byte int long float..number, date , boolean , char,String
36. Method getMethod = obj1.getClass().getMethod("compareTo", new Class\[] { cl });
37. return (Integer) getMethod.invoke(obj1, new Object\[] { obj2 });
38. }
39. return 0;//默认不排序
40. }
41. };
42. }