2016-07-05 51 views
-3
ArrayList <ShyForeignStockHolding> aithird = new ArrayList<ShyForeignStockHolding>(); 

ShyForeignStockHolding shyfrst = new ShyForeignStockHolding (shyPurchaseSharePrice, shyCurrentSharePrice, shyNumberOfShares, shyCompanyName, shyconversionRate1); 

aithird.add(shyfrst); 

我想Currentshare Price如何對最低值的數組列表對象進行排序?

+0

shyCurrentSharePrice是浮點型變量 – user3578882

+0

您可以使用java比較器來實現你想要的。 –

+0

你能給我一些提示或示例嗎? – user3578882

回答

0

排序ArrayList您可以使用對象的自定義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 
    } 

}); 
0

與Java 8中,您現在可以輕鬆創建一個Comparator(完全避免匿名類)。由於這是按升序排列,因此您可以在該比較器上調用reversed()使其降序。

Comparator<ShyForeignStockHolding> comparator = Comparator.comparing((sh -> sh.getTheVarYouWantToCompareBy())).reversed(); 
aithird.sort(comparator); 
相關問題