首页 > 解决方案 > 更改列表中的项目并排序然后列出

问题描述

我想访问一个对象列表并更改列表中的一些值。之后我想根据日期对列表进行排序。有可能做到这一点,我该怎么做?

这是我的代码:

public class AccountTransactions {
    private int transactionID;
    private int transactionCode;
    private String transactionDate;
    private String transactionType;
    private String transactionTime;
    private String transactionAmount;
    private String transactionTo;
    private String transactionFrom;

    ... // for bravety

}

Transactions.java

  public  ListtransactionsObjects getUserTransactions(SearchbyPublicKeyObject publicKeyObject){

        List<AccountTransactions> transactionFrom =  TransactionsRepository.findByTransactionFrom(publicKeyObject.getPublicKey());
        List<AccountTransactions> transactionTo = TransactionsRepository.findByTransactionTo(publicKeyObject.getPublicKey());



  //sudo code
  // loop through list 
  // transactionTo.transactionAmount = "+" + transactionAmount



        List<AccountTransactions> AllTransactions = Stream.concat(transactionFrom.stream(), transactionTo.stream())
                             .collect(Collectors.toList());
    }



 // sort AllTransactions by transactionDate which is of type 05/06/2018

标签: javalist

解决方案


Collections.sort用作_

// sort AllTransactions by transactionDate which is of type 05/06/2018
Collections.sort(AllTransactions, AccountTransactions::getTransactionDate);

假设您应该getTransactionDateAccountTransactions其中返回您的交易date(而不是string您拥有的交易)。您可能需要将您的转换stringdate.


推荐阅读