简体   繁体   English

Java 按两个 BigDecimal 之间的差值排序

[英]Java sorting by spread between two BigDecimal

Please help with sorting in Java. I have a simple example (looks like this).请Java帮忙排序。我有一个简单的例子(看起来像这样)。 I need to sort list by the difference between two BigDecimals.我需要根据两个 BigDecimals 之间的差异对列表进行排序。

My Data class (I cut getters)我的数据 class(我剪了吸气剂)

    public class Quotation {
    private final long id;
    private final BigDecimal askPrice;
    private final BigDecimal bidPrice;

    public Quotation(long id, BigDecimal bidPrice, BigDecimal askPrice) {
        this.id = id;
        this.askPrice = askPrice;
        this.bidPrice = bidPrice;
    }
    }

Here we store our records.我们在这里存储我们的记录。

        storeQuotation(new Quotation(1000,new BigDecimal("99"), new BigDecimal("104")));
        storeQuotation(new Quotation(1001,new BigDecimal("69"), new BigDecimal("72")));
        storeQuotation(new Quotation(1002,new BigDecimal("65"), new BigDecimal("69")));
        storeQuotation(new Quotation(1003,new BigDecimal("70"), new BigDecimal("71")));
        storeQuotation(new Quotation(1004,new BigDecimal("71"), new BigDecimal("73")));
        storeQuotation(new Quotation(1005,new BigDecimal("90"), new BigDecimal("95")));
        storeQuotation(new Quotation(1006,new BigDecimal("92"), new BigDecimal("93")));
        storeQuotation(new Quotation(1007,new BigDecimal("94"), new BigDecimal("98")));
        storeQuotation(new Quotation(1008,new BigDecimal("90"), new BigDecimal("92")));
        storeQuotation(new Quotation(1009,new BigDecimal("92"), new BigDecimal("95")));
out - Not Sorted
    id - 1000.   Bid - 99.   Ask - 104.   Spread = 5
    id - 1001.   Bid - 69.   Ask - 72.   Spread = 3
    id - 1002.   Bid - 65.   Ask - 69.   Spread = 4
    id - 1003.   Bid - 70.   Ask - 71.   Spread = 1
    id - 1004.   Bid - 71.   Ask - 73.   Spread = 2
    id - 1005.   Bid - 90.   Ask - 95.   Spread = 5
    id - 1006.   Bid - 92.   Ask - 93.   Spread = 1
    id - 1007.   Bid - 94.   Ask - 98.   Spread = 4
    id - 1008.   Bid - 90.   Ask - 92.   Spread = 2
    id - 1009.   Bid - 92.   Ask - 95.   Spread = 3

And I just need to sort this list by the difference between bidPrice and askPrice.我只需要根据 bidPrice 和 askPrice 之间的差异对这个列表进行排序。 I tried this method...我试过这个方法...

    public static List<Quotation> getSpreadsList(boolean decreasing) {
        List<Quotation> sortedBySpread = QuotationsStoreImpl.quotationList;

        Collections.sort(sortedBySpread, (a, b) ->
     (a.getBidPrice().intValue() - b.getAskPrice().intValue()));

//        sortedBySpread.sort((a, b) -> 
//    (a.getBidPrice().intValue() - b.getAskPrice().intValue()));

        if (decreasing) {
            Collections.reverse(sortedBySpread);
        }
        return sortedBySpread;
    }
    }

But without success...但是没有成功...

out - Sorted
    id - 1002.   Bid - 65.   Ask - 69.   Spread = 4
    id - 1003.   Bid - 70.   Ask - 71.   Spread = 1
    id - 1004.   Bid - 71.   Ask - 73.   Spread = 2
    id - 1001.   Bid - 69.   Ask - 72.   Spread = 3
    id - 1008.   Bid - 90.   Ask - 92.   Spread = 2
    id - 1009.   Bid - 92.   Ask - 95.   Spread = 3
    id - 1006.   Bid - 92.   Ask - 93.   Spread = 1
    id - 1007.   Bid - 94.   Ask - 98.   Spread = 4
    id - 1005.   Bid - 90.   Ask - 95.   Spread = 5
    id - 1000.   Bid - 99.   Ask - 104.   Spread = 5

The list is mixed but not sorted according to my criteria !该列表是混合的,但未根据我的标准排序! Spread not sorted !传播未分类!

How can I sort this list correct, by spread?我怎样才能按传播正确地排序这个列表?

I don't have much experience in java. And all my attempts have come to nothing.我在 java 方面经验不多。而且我所有的尝试都没有结果。

Collections.sort(sortedBySpread, (a, b) -> (a.getBidPrice().intValue() - b.getAskPrice().intValue())); does some math that makes little since since it subtracts the ask from the bid of two different quotes.做一些没什么用的数学运算,因为它从两个不同报价的出价中减去要价。

Instead you should calculate the spread of a and then subtract the spread of b :相反,您应该计算a的传播,然后减去b的传播:

Collections.sort(sortedBySpread, (a, b) -> (a.getBidPrice().intValue() - a.getAskPrice().intValue()) - (b.getBidPrice().intValue() - b.getAskPrice().intValue()));

Generally this could be expanded into the following to make it more clear what is going on:通常,这可以扩展为以下内容,以更清楚地说明发生了什么:

Collections.sort(sortedBySpread, (Quotation a, Quotation b) -> {
    int spreadA = a.getBidPrice().intValue() - a.getAskPrice().intValue();
    int spreadB = b.getBidPrice().intValue() - b.getAskPrice().intValue();
    return spreadA - spreadB;
});

But starting with the first snippet IntelliJ suggest the arguably far cleaner solution但是从第一个片段开始,IntelliJ 提出了可以说是更简洁的解决方案

Collections.sort(sortedBySpread, Comparator.comparingInt(a -> (a.getBidPrice().intValue() - a.getAskPrice().intValue())));

And going from there it might make sense to have a getSpread on Quotation :从那里开始,在Quotation上有一个getSpread可能是有意义的:

public int getSpread() {
    return bidPrice.intValue() - askPrice.intValue();
}

which would then allow这将允许

Collections.sort(sortedBySpread, Comparator.comparingInt(Quotation::getSpread));

And finally最后

sortedBySpread.sort(Comparator.comparingInt(Quotation::getSpread));

without the need for Collections.sort .不需要Collections.sort

For demo, I put your values in a list.对于演示,我将您的值放在列表中。 I also added the following to your class.我还将以下内容添加到您的 class。

public BigDecimal getSpread() {
       return askPrice.subtract(bidPrice);
}
public String toString() {
       return "id - %d    bid - %6.2f     ask - %6.2f     spread - %6.2f".formatted(id, bidPrice, askPrice,getSpread());   
}

The data数据

 List<Quotation> quotes = new ArrayList<>(List.of(
   (new Quotation(1000,new BigDecimal("99"), new BigDecimal("104"))),
   (new Quotation(1001,new BigDecimal("69"), new BigDecimal("72"))),
   (new Quotation(1002,new BigDecimal("65"), new BigDecimal("69"))),
   (new Quotation(1003,new BigDecimal("70"), new BigDecimal("71"))),
   (new Quotation(1004,new BigDecimal("71"), new BigDecimal("73"))),
   (new Quotation(1005,new BigDecimal("90"), new BigDecimal("95"))),
   (new Quotation(1006,new BigDecimal("92"), new BigDecimal("93"))),
   (new Quotation(1007,new BigDecimal("94"), new BigDecimal("98"))),
   (new Quotation(1008,new BigDecimal("90"), new BigDecimal("92"))),
   (new Quotation(1009,new BigDecimal("92"), new BigDecimal("95")))));

The sorting part is simple.排序部分很简单。 Just use a comparator, referencing the spread.只需使用一个比较器,参考价差。

quotes.sort(Comparator.comparing(Quotation::getSpread));

And print并打印

for(Quotation q : quotes) {
    System.out.println(q);
} 

Prints印刷

id - 1003    bid -  70.00     ask -  71.00     spread -   1.00
id - 1006    bid -  92.00     ask -  93.00     spread -   1.00
id - 1004    bid -  71.00     ask -  73.00     spread -   2.00
id - 1008    bid -  90.00     ask -  92.00     spread -   2.00
id - 1001    bid -  69.00     ask -  72.00     spread -   3.00
id - 1009    bid -  92.00     ask -  95.00     spread -   3.00
id - 1002    bid -  65.00     ask -  69.00     spread -   4.00
id - 1007    bid -  94.00     ask -  98.00     spread -   4.00
id - 1000    bid -  99.00     ask - 104.00     spread -   5.00
id - 1005    bid -  90.00     ask -  95.00     spread -   5.00

      

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM