オブジェクトの属性別にソート
1.comparableインタフェースはjavaです.langクラスで、Comparatorインタフェースはjavaです.utilクラスの.2.Comparableは集合内部で定義されたメソッド実装のソートであり、Comparatorは集合外部で実装されたソートであるため、ソートを実現するには集合外でComparatorインタフェースを定義する方法または集合内でComparableインタフェースを実装する方法が必要である.カスタムクラスでComparableインタフェースを実装すると、このクラスにはソート機能があり、Comparableとソートするクラスのインスタンスがあります.
Comparatorは比較的柔軟で、クラスにバインドされていません.カスタムクラスを実現するには、ソート方法やソートルールしか定義されていません.言うまでもなく、この方式は比較的柔軟である.私たちのソートするクラスは、それぞれ複数のComparatorインタフェースを実装するクラスとバインドすることができ、それによって自分の意思で複数の方法でソートできる目的を達成することができます.Comparable-「静的バインドソート」、Comparator-「動的バインドソート」.
並べ替え:
1番目のパラメータが2番目のパラメータより小さい、等しい、または大きいことに基づいて、それぞれ負の整数、ゼロ、または正の整数を返します.
属性はString型:(オブジェクト属性を動的にロードするgetメソッド)
m1:
完全な接続:
1.http://wenku.baidu.com/view/a19a50ea81c758f5f61f674a.html
2.http://jardot.iteye.com/blog/762349
DayReport.JAvaクラス:
1、Double型データの並べ替え(共通方法):
2.Intタイプのソート(一般的な方法)
次の操作を行います.
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();
}
});
1番目のパラメータが2番目のパラメータより小さい、等しい、または大きいことに基づいて、それぞれ負の整数、ゼロ、または正の整数を返します.
属性は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;
else
return 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);