0

I'm not very good with sorting and I need help on this one

How do I sort this Arraylist depending on date field

Controller:

 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>

I'd like to know how to sort these data using Date Opened field. Thanks in advance.

iamjpcbau
  • 374
  • 1
  • 11
  • 29

1 Answers1

0

You need to create a comparator like:

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

This comparator will decide, logic that you want to use to sort the list. So here i just have date field that will be used.

And then sort list after you populated using:

 Collections.sort(myformList, ..AccountCustomerFormComparator instance);
SMA
  • 36,381
  • 8
  • 49
  • 73