基于具有一定条件的对象一个属性的Java排序

时间:2016-10-15 18:03:25

标签: java sorting

我在java中面临一个排序问题。我有一个事务bean列表,我的要求是根据不同的属性进行排序,我能够做到。 现在问题是,我在bean中称为“ trnAmt ”的属性,它具有借方和贷方金额。我需要在debitAmt和creditAmt上进行排序。金额类型将决定bean其他属性 trnType 属性,如果trnType为'C'表示它是借方金额,或者如果trnType为'D'则表示它是借方金额。

这是我的豆子

public class CardTransaction {

    private String trnType;
    private Date trnBookingDate;
    private String trnAmt;
    private List trnDesc;



---getter n setter method..
}

Here is my method which is failing .

public void applyColumnSorting(final List transDtlsList, final Sorting sorting) {
        final String sortOrder = sorting.getSortOrder();
        final String column = sorting.getSortCol();
        if ("DT".equalsIgnoreCase(column)) {

            Collections.sort(transDtlsList, new Comparator() {
                private final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

                @Override
                public int compare(final CardTransaction s, final CardTransaction d) {
                    Date sdate = null, ddate = null;
                    sdate = s.getTrnBookingDate();
                    ddate = d.getTrnBookingDate();
                    int ret = 0;
                    if ("A".equalsIgnoreCase(sortOrder)) {
                        ret = ddate.before(sdate) ? 1 : -1;
                    } else {
                        ret = sdate.before(ddate) ? 1 : -1;
                    }
                    return ret;
                }
            });
        } else if ("NR".equalsIgnoreCase(column)) {
            Collections.sort(transDtlsList, new Comparator() {

                @Override
                public int compare(final CardTransaction s, final CardTransaction d) {
                    String sDesc = StringUtils.arrayToDelimitedString(s.getTrnDesc().toArray(), "");
                    String dDesc = StringUtils.arrayToDelimitedString(d.getTrnDesc().toArray(), "");

                    int ret = 0;
                    if ("A".equalsIgnoreCase(sortOrder)) {
                        ret = sDesc.compareTo(dDesc);
                    } else {
                        ret = dDesc.compareTo(sDesc);
                    }
                    return ret;
                }
            });
        } else if ("CT".equalsIgnoreCase(column)) { // Sorting for credit amt.
            Collections.sort(transDtlsList, new Comparator() {

                @Override
                public int compare(final CardTransaction s, final CardTransaction d) {
                    int ret = 0;
                    if(s.getTrnType().equals("C")  ) {
                        final BigDecimal samt =
                            new BigDecimal(org.apache.commons.lang.StringUtils.isNotBlank(s.getOriginalAmt()) ? s.getOriginalAmt() : "0.00");
                        final BigDecimal damt =
                            new BigDecimal(org.apache.commons.lang.StringUtils.isNotBlank(d.getOriginalAmt()) ? d.getOriginalAmt() : "0.00");
                        if ("A".equalsIgnoreCase(sortOrder)) {
                            ret = samt.compareTo(damt);
                        } else {
                            ret = damt.compareTo(samt);
                        }
                        return ret;
                        }
                        return ret;

                }
            });
        }else if ("DA".equalsIgnoreCase(column)) { //sorting for debit amount
            Collections.sort(transDtlsList, new Comparator() {

                @Override
                public int compare(final CardTransaction s, final CardTransaction d) {

                    int ret = 0;
                    if(s.getTrnType().equals("D")  ) {
                    final BigDecimal samt =
                        new BigDecimal(org.apache.commons.lang.StringUtils.isNotBlank(s.getOriginalAmt()) ? s.getOriginalAmt() : "0.00");
                    final BigDecimal damt =
                        new BigDecimal(org.apache.commons.lang.StringUtils.isNotBlank(d.getOriginalAmt()) ? d.getOriginalAmt() : "0.00");
                    if ("A".equalsIgnoreCase(sortOrder)) {
                        ret = samt.compareTo(damt);
                    } else {
                        ret = damt.compareTo(samt);
                    }
                    return ret;
                    }
                    return ret;
                }
            });
        }

    }

0 个答案:

没有答案