【问题标题】:How to sort Arraylist by date then pass it to JSP如何按日期对Arraylist进行排序然后将其传递给JSP
【发布时间】:2015-05-30 02:07:29
【问题描述】:

我不太擅长排序,我需要这方面的帮助

如何根据日期字段对这个 Arraylist 进行排序

控制器:

 Set<CustomerAccount> cas = customer.getCustomerAccounts();

    if ( !cas.isEmpty() )
    {
        // initialize form storage for accounts
        List<AccountCustomerForm> accntForms = new ArrayList<AccountCustomerForm>( cas.size() );

        AccountCustomerForm accntForm = null;
        for ( CustomerAccount ca : cas )
        {

            // if there are more than 1 account linked to the customer's account, get all related accounts
            // else only include the customer's account
            if ( ca.getAccount().getCustomerAccounts().size() > 0 )
            {
                AccountCustomerForm jointAccountForm = null;
                for ( CustomerAccount jointAccount : ca.getAccount().getCustomerAccounts() )
                {
                    jointAccountForm = new AccountCustomerForm();
                    jointAccountForm.setAccountNumber( jointAccount.getAccount().getAccountNumber() );
                    jointAccountForm.setAccountName( jointAccount.getCustomer().getName() );
                    jointAccountForm.setDateOpened( jointAccount.getAccount().getDateOpened() ); //as per sir del, date opened is supposed to be seen in linked accounts -jpcbautista
                    jointAccountForm.setStatus( jointAccount.getAccount().getStatus() );
                    jointAccountForm.setJoint( jointAccount.getAccount().isJoint() );
                    jointAccountForm.setProductName( jointAccount.getAccount().getProductMatrix().getProductName() );

                    accntForms.add( jointAccountForm );
                }

            }

JSP:

<c:forEach items="${bean.accounts}" var="accnt" varStatus="status" begin="0">
                <c:choose> 
                    <c:when test="${status.count % 2 == 0}"><tr></c:when>
                    <c:otherwise><tr bgcolor="#BFDFFF"></c:otherwise>
                </c:choose>
                        <td>${accnt.accountNumber}</td>
                        <td>${accnt.accountName}</td>
                        <td>
                                    ${accnt.dateOpened}</td>
                        <td>${accnt.status}</td>
                        <c:choose>
                            <c:when test = "${accnt.joint}" > <td> Yes </td> </c:when>
                            <c:otherwise> <td> No</td></c:otherwise>
                        </c:choose>
                        <td>${accnt.productName}</td>
                        <td></td>
                        <!-- <td></td> -->
                </c:forEach>

我想知道如何使用“打开日期”字段对这些数据进行排序。提前致谢。

【问题讨论】:

  • 您可以使用Collections.sort 进行排序。只需提供您自己的Comparator

标签: java javascript jsp sorting arraylist


【解决方案1】:

您需要创建一个比较器,例如:

 public class AccountCustomerFormComparator implements Comparator<AccountCustomerForm> {
      public int compare(AccountCustomerForm form1, AccountCustomerForm form2)            {
          return form1.getDate().compareTo(form2.getDate());           
      }
 }

此比较器将决定您要用于对列表进行排序的逻辑。所以在这里我只有将要使用的日期字段。

然后sort 使用以下方式填充后的列表:

 Collections.sort(myformList, ..AccountCustomerFormComparator instance);

【讨论】:

    【解决方案2】:

    使用Collections.sort(List, Comparator) 方法:

    Collections.sort(customerAccountList, new CustomerComparator());
    

    创建Comparator,如下所示。 注意:这只是一个例子。

    public class CustomerComparator implements Comparator<AccountCustomerForm>
    {
      public int compare(AccountCustomerForm a, AccountCustomerForm b){
        // your condition for checking your opened date
        //Returns a negative integer, zero, or a positive integer as the first argument
        // is less than, equal to, or greater than the second.
        return 1;
    }
    

    【讨论】:

    • 对不起,我还是一头雾水,能否赐教。我应该提出什么条件?我应该创建一个包含日期的 if-else 语句吗?
    • 是的,你有两个对象可以使用任何条件,只要确保返回正确的返回值。
    猜你喜欢
    • 2017-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-16
    • 2017-05-26
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多