ArrayList <ShyForeignStockHolding> aithird = new ArrayList<ShyForeignStockHolding>();
ShyForeignStockHolding shyfrst = new ShyForeignStockHolding (shyPurchaseSharePrice, shyCurrentSharePrice, shyNumberOfShares, shyCompanyName, shyconversionRate1);
aithird.add(shyfrst);
我想按ArrayList
Currentshare Price
进行排序
答案 0 :(得分:0)
您可以使用对象的自定义Comparator
,假设您使用的是shyCurrentSharePrice
的双倍:
List<ShyForeignStockHolding> sFSH = new ArrayList<ShyForeignStockHolding>();
//add Your objects
Collections.sort(sFSH, new Comparator<ShyForeignStockHolding>() {
@Override public double compare(ShyForeignStockHolding ssh1, ShyForeignStockHolding ssh2) {
return ssh1.shyCurrentSharePrice + shh2.shyCurrentSharePrice; //descending use - for Ascending
}
});
答案 1 :(得分:0)
使用Java 8,您现在可以轻松地创建一个Comparator(完全避免使用匿名类)。由于这是按升序排列的,因此您可以在该比较器上调用reversed()
使其降序。
Comparator<ShyForeignStockHolding> comparator = Comparator.comparing((sh -> sh.getTheVarYouWantToCompareBy())).reversed();
aithird.sort(comparator);