我想访问对象列表并更改列表中的一些值。之后我想根据日期对列表进行排序。是否可以这样做,我该怎么做呢?
这是我的代码:
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
答案 0 :(得分:2)
使用Collections.sort
作为
// sort AllTransactions by transactionDate which is of type 05/06/2018
Collections.sort(AllTransactions, AccountTransactions::getTransactionDate);
假设您getTransactionDate
中的AccountTransactions
返回您的交易date
(而不是您拥有的string
)。
您可能需要将string
转换为date.
答案 1 :(得分:2)
遍历循环中的对象并设置值。然后使用比较器。
Collections.sort(list, new Comparator<AccountTransactions>() {
@Override
public int compare(AccountTransactions o1, AccountTransactions o2) {
return o1.getTransactionDate().compareTo(o2.getTransactionDate());
}
});
制作日期类型的transactionDate。