按对象中某一属性排序
来源:互联网 发布:聊天有哪些软件 编辑:程序博客网 时间:2024/05/22 08:17
1.Comparable接口是在java.lang类中的,而Comparator接口是在java.util类中的。
2.Comparable 是在集合内部定义的方法实现的排序,Comparator 是在集合外部实现的排序,所以,如想实现排序,就需要在集合外定义 Comparator 接口的方法或在集合内实现 Comparable 接口的方法。
用自定义类实现 Comparable接口,那么这个类就具有排序功能,Comparable和具体你要进行排序的类的实例邦定。
而Comparator比较灵活,它没有和任何类绑定,实现它的自定义类仅仅定义了一种排序方式或排序规则。不言而喻,这种方式比较灵活。我们的要排序的类可以分别和多个实现 Comparator接口的类绑定,从而达到可以按自己的意愿实现按多种方式排序的目的。Comparable——“静态绑定排序”,Comparator——“动态绑定排序”。
排序:
Collections.sort(lists, new comparator(){ int compare(Object o1, Object o2) { Member m1=(Member)o1; Member m2=(Member)o2; // 按姓名排序 return m1.getName().compareTo(m2.getName(); } });
根据第一个参数小于、等于或大于第二个参数分别返回负整数、零或正整数
属性为String型:(动态加载对象属性的get方法)
1. public class SortList<E>{ 2. public void Sort(List<E> list, final String method, final String sort){ 3. Collections.sort(list, new Comparator() { 4. public int compare(Object a, Object b) { 5. int ret = 0; 6. try{ 7. Method m1 = ((E)a).getClass().getMethod(method, null); 8. Method m2 = ((E)b).getClass().getMethod(method, null); 9. if(sort != null && "desc".equals(sort))//倒序 10. ret = m2.invoke(((E)b), null).toString().compareTo(m1.invoke(((E)a), null).toString()); 11. else//正序 12. ret = m1.invoke(((E)a), null).toString().compareTo(m2.invoke(((E)b), null).toString()); 13. }catch(NoSuchMethodException ne){ 14. System.out.println(ne); 15. }catch(IllegalAccessException ie){ 16. System.out.println(ie); 17. }catch(InvocationTargetException it){ 18. System.out.println(it); 19. } 20. return ret; 21. } 22. }); 23. } 24. }
m1:
public java.lang.Integer com.openb2c.openreport.DayReport.getOrderSize()
完整连接:
1.http://wenku.baidu.com/view/a19a50ea81c758f5f61f674a.html
2.http://jardot.iteye.com/blog/762349
DayReport.java类:
public class DayReport extends BaseReport{private String date;private Double totalFee;private Double refundFee;private Double getMoney;private Double postFee;private Integer orderSize;private Long productNum; //产品数,即符合条件的订单中有多少个产品private Integer backProductNum; //退款通过的订单中产品的个数private Integer backOrderNum; //退款通过的订单个数public String getDate() {return date;}public void setDate(String date) {this.date = date;}public Double getTotalFee() {return totalFee;}public void setTotalFee(Double totalFee) {this.totalFee = totalFee;}public Double getRefundFee() {return refundFee;}public void setRefundFee(Double refundFee) {this.refundFee = refundFee;}public Double getGetMoney() {return getMoney;}public void setGetMoney(Double getMoney) {this.getMoney = getMoney;}public Double getPostFee() {return postFee;}public void setPostFee(Double postFee) {this.postFee = postFee;}public Integer getOrderSize() {return orderSize;}public void setOrderSize(Integer orderSize) {this.orderSize = orderSize;}public Long getProductNum() {return productNum;}public void setProductNum(Long productNum) {this.productNum = productNum;}public Integer getBackProductNum() {return backProductNum;}public void setBackProductNum(Integer backProductNum) {this.backProductNum = backProductNum;}public Integer getBackOrderNum() {return backOrderNum;}public void setBackOrderNum(Integer backOrderNum) {this.backOrderNum = backOrderNum;}}
1、对Double型数据排序(通用方法):
/** * 给Double型排序 * method 为DayReport中属性的 get方法名如:getOrderSize * sort 为排序 *check 为选择的条件如1天内、1月内等 */@SuppressWarnings("unchecked")public static void SortDouble (List<DayReport> list,final String method,final String sort){Comparator comparator = new Comparator(){public int compare(Object a, Object b) {int ret = 0; try { Method m1 = ((DayReport)a).getClass().getMethod(method, null); Method m2 = ((DayReport)b).getClass().getMethod(method, null); double m=(Double) m1.invoke((DayReport)a); double n=(Double) m2.invoke((DayReport)b); double c=m-n;if(c>0.0)//倒序 {if(sort.equals("DESC")){ret=-1; //本来}else{ret=1;}}else if(c<0.0){if(sort.equals("DESC")){ret=1;}else{ret=-1;}}else{ret=0;}} catch (SecurityException e) {StringBuilder msg = new StringBuilder("Double类型数据排序时出现异常!");Debug.logError(e,msg.toString(), module);} catch (NoSuchMethodException e) {StringBuilder msg = new StringBuilder("Double类型数据排序时出现异常!");Debug.logError(e,msg.toString(), module);} catch (IllegalAccessException e) {StringBuilder msg = new StringBuilder("Double类型数据排序时出现异常!");Debug.logError(e,msg.toString(), module);} catch (InvocationTargetException e) {StringBuilder msg = new StringBuilder("Double类型数据排序时出现异常!");Debug.logError(e,msg.toString(), module);} return ret;}};Collections.sort(list,comparator);}
2.对Int型排序(通用方法)
/** * 给int型排序 */@SuppressWarnings("unchecked")public static void SortInt (List<DayReport> list,final String method,final String sort){/*Comparator comparator = new Comparator(){public int compare(Object a, Object b) {Integer aa = ((DayReport) a).getOrderSize();Integer bb = ((DayReport) b).getOrderSize();Integer cc = aa - bb;if (cc > 0)return 1;if (cc < 0)return -1;elsereturn 0;}};Collections.sort(list,comparator);}*/Comparator comparator = new Comparator() {public int compare(Object a, Object b) {int ret = 0;Method m1 = ((DayReport) a).getClass().getMethod(method,null);Method m2 = ((DayReport) b).getClass().getMethod(method,null);Integer m = (Integer) m1.invoke((DayReport) a);Integer n = (Integer) m2.invoke((DayReport) b);Integer c = m - n;if (c > 0)// 倒序{if (sort.equals("DESC")) {ret = -1; // 本来} else {ret = 1;}} else if (c < 0) {if (sort.equals("DESC")) {ret = 1;} else {ret = -1;}} else {ret = 0;}return ret;}};Collections.sort(list, comparator);}
使用:
<th><a href="<@ofbizUrl>queryDay?check=${check?if_exists}&method=getTotalFee&sort=${orderby?if_exists}</@ofbizUrl>">销售额</a></th> <th><a href="<@ofbizUrl>queryDay?check=${check?if_exists}&method=getRefundFee&sort=${orderby?if_exists}</@ofbizUrl>">退款额</a></th>
String method=(String)context.get("method"); String sort=(String)context.get("sort"); if(UtilValidate.isEmpty(sort)){ sort="ASC"; }else{if (UtilValidate.areEqual(sort, "ASC")) {sort = "DESC";} else {sort = "ASC";} } if(UtilValidate.isNotEmpty(method)){ if(method.equals("getOrderSize")){ DayReportService.SortInt(finallyList,method,sort); }else{ DayReportService.SortDouble(finallyList,method,sort); } } result.put("finallyList", finallyList);result.put("sort", sort);result.put("check", check);
0 0
- 按对象中某一属性排序
- 集合中根据对象中的某一属性属性排序
- LinkedList中将对象按照某一属性排序
- LinkedList中将对象按照某一属性排序
- LinkedList中将对象按照某一属性排序
- js 数组对象根据某一属性排序
- 按照对象的某一属性进行排序
- 根据对象某一属性进行排序
- list排序,根据list中存放对象的某一属性排序
- Java面试题,List集合中根据对象的某一属性排序
- web开发中如何对json对象的某一属性对其进行排序
- 根据对象中某一属性进行升序降序排序的javascript实现
- 将List中的对象按对象某一属性的大小来排序
- js -- 对象排序(根据对象的某一属性)
- 根据某一属性对数组中的对象进行排序
- List 中对象按属性排序
- Python3对一个class类型的list按class中某一属性排序
- 数组排序(按对象中某个属性进行排序)
- JavaScript学习笔记之强大的自动类型转换
- 数组的相关操作
- javaScript中的对象、BOM、DOM知识点总结
- Linux学习之开始
- 如何让一张图片在手机端页面占满整个屏幕不带滚动条(示例)
- 按对象中某一属性排序
- 感知机
- MSSQL树型结构/获得上级及上上级及获得下级及下下级
- iReport输出为HTML或PDF、XLS、DOC【输出HTML两种方法】
- python sys.argv[]
- iReport默认参数和变量的含义解析【转】
- Spark ML -1:ALS推荐算法
- iReport使用教程[转]+总结+模版
- ofbiz系统中Mysql