-3
ArrayList <ShyForeignStockHolding> aithird = new ArrayList<ShyForeignStockHolding>();

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

aithird.add(shyfrst);

I want to sort ArrayList by Currentshare Price

currarpickt
  • 2,290
  • 4
  • 24
  • 39

2 Answers2

0

You can use a custom Comparator of your Object, assuming that you are using double for the 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
    }

});
Ahmad Sanie
  • 3,678
  • 2
  • 21
  • 56
0

With Java 8, you can now create a Comparator easily like so (avoiding anonymous classes entirely). Since this is in ascending order, you can then call reversed() on that Comparator to make it descending.

Comparator<ShyForeignStockHolding> comparator = Comparator.comparing((sh -> sh.getTheVarYouWantToCompareBy())).reversed();
aithird.sort(comparator);
ifly6
  • 5,003
  • 2
  • 24
  • 47