Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何对值最低的arraylist对象进行排序? ArrayList aithird=new ArrayList(); ShyForeignStockHolding shyfrst=新的ShyForeignStockHolding(shyPurchaseSharePrice、shyCurrentSharePrice、shyNumber of Shares、shyCompanyName、shyconversionRate1); aithird.add(shyfrst);_Java_Sorting_Arraylist - Fatal编程技术网

Java 如何对值最低的arraylist对象进行排序? ArrayList aithird=new ArrayList(); ShyForeignStockHolding shyfrst=新的ShyForeignStockHolding(shyPurchaseSharePrice、shyCurrentSharePrice、shyNumber of Shares、shyCompanyName、shyconversionRate1); aithird.add(shyfrst);

Java 如何对值最低的arraylist对象进行排序? ArrayList aithird=new ArrayList(); ShyForeignStockHolding shyfrst=新的ShyForeignStockHolding(shyPurchaseSharePrice、shyCurrentSharePrice、shyNumber of Shares、shyCompanyName、shyconversionRate1); aithird.add(shyfrst);,java,sorting,arraylist,Java,Sorting,Arraylist,我想按Currentshare Price对ArrayList排序您可以使用对象的自定义比较器,假设您对shyCurrentSharePrice使用double: ArrayList <ShyForeignStockHolding> aithird = new ArrayList<ShyForeignStockHolding>(); ShyForeignStockHolding shyfrst = new ShyForeignStockHolding (shyPurch

我想按
Currentshare Price
ArrayList
排序您可以使用对象的自定义
比较器,假设您对
shyCurrentSharePrice
使用double:

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

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

aithird.add(shyfrst);
List sFSH=new ArrayList();
//添加您的对象
Collections.sort(sFSH,newcomparator(){
@覆盖公共双比较(ShyForeignStockHolding ssh1、ShyForeignStockHolding ssh2){
返回ssh1.shyCurrentSharePrice+shh2.shyCurrentSharePrice;//降序使用-升序使用
}
});

使用Java 8,您现在可以像这样轻松地创建比较器(完全避免匿名类)。因为这是按升序排列的,所以可以在该比较器上调用
reversed()
,使其降序

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
    }

});
Comparator Comparator=Comparator.comparating((sh->sh.getTheVarYouWantToCompareBy()).reversed();
aithird.sort(比较器);

shyCurrentSharePrice是浮点型变量。你可以使用java比较器来实现你想要的。你能给我一些提示吗?或者举个例子?
Comparator<ShyForeignStockHolding> comparator = Comparator.comparing((sh -> sh.getTheVarYouWantToCompareBy())).reversed();
aithird.sort(comparator);