我对排序不是很好,我需要帮助这个
如何根据日期字段对此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>
我想知道如何使用“打开日期”字段对这些数据进行排序。提前谢谢。
答案 0 :(得分:0)
使用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;
}
答案 1 :(得分:0)
您需要创建一个比较器,如:
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);