Java 如何按日期对Arraylist排序,然后将其传递给JSP

Java 如何按日期对Arraylist排序,然后将其传递给JSP,java,javascript,jsp,sorting,arraylist,Java,Javascript,Jsp,Sorting,Arraylist,我不太擅长分类,这方面我需要帮助 如何根据日期字段对此Arraylist排序 控制器: Set<CustomerAccount> cas = customer.getCustomerAccounts(); if ( !cas.isEmpty() ) { // initialize form storage for accounts List<AccountCustomerForm> accntForms = new Ar

我不太擅长分类,这方面我需要帮助

如何根据日期字段对此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 );
                }

            }
Set cas=customer.getCustomerAccounts();
如果(!cas.isEmpty())
{
//初始化帐户的窗体存储
List accnforms=newarraylist(cas.size());
AccountCustomPerform accntForm=null;
对于(客户帐户ca:cas)
{
//如果有多个帐户链接到客户的帐户,请获取所有相关帐户
//其他仅包括客户的帐户
如果(ca.getAccount().getCustomerAccounts().size()>0)
{
AccountCustomPerform jointAccountForm=null;
对于(CustomerAccount jointAccount:ca.getAccount().getCustomerAccounts())
{
jointAccountForm=新帐户CustomPerform();
jointAccountForm.setAccountNumber(jointAccount.getAccount().getAccountNumber());
jointAccountForm.setAccountName(jointAccount.getCustomer().getName());
jointAccountForm.SetDateOpen(jointAccount.getAccount().GetDateOpen());//根据sir del,打开的日期应该在链接帐户-jpcbautista中看到
jointAccountForm.setStatus(jointAccount.getAccount().getStatus());
jointAccountForm.setJoint(jointAccount.getAccount().isJoint());
jointAccountForm.setProductName(jointAccount.getAccount().getProductMatrix().getProductName());
accnforms.add(jointAccountForm);
}
}
JSP:


${accnt.accountNumber}
${accnt.accountName}
${accnt.dateOpened}
${accnt.status}
对
不
${accnt.productName}
我想知道如何使用“打开日期”字段对这些数据进行排序。提前感谢。

使用以下方法:

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

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;
}
公共类CustomerComparator实现Comparator
{
公共整数比较(AccountCustomPerform a、AccountCustomPerform b){
//您检查开启日期的条件
//返回负整数、零或正整数作为第一个参数
//小于、等于或大于第二个。
返回1;
}

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

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

您可以使用
集合进行排序。排序
。只需提供您自己的
比较器即可。很抱歉,我仍然感到困惑,您可以告诉我。我应该设置什么条件?我是否应该创建包含日期的if-else语句?是的,您有两个对象可以使用任何条件,只需确保返回正确的返回值。您可以使用e这个例子
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;
}
 public class AccountCustomerFormComparator implements Comparator<AccountCustomerForm> {
      public int compare(AccountCustomerForm form1, AccountCustomerForm form2)            {
          return form1.getDate().compareTo(form2.getDate());           
      }
 }
 Collections.sort(myformList, ..AccountCustomerFormComparator instance);