diff --git a/desktop/src/main/java/bisq/desktop/main/market/offerbook/OfferBookChartViewModel.java b/desktop/src/main/java/bisq/desktop/main/market/offerbook/OfferBookChartViewModel.java index c7c6bf76376..453eeab6f7f 100644 --- a/desktop/src/main/java/bisq/desktop/main/market/offerbook/OfferBookChartViewModel.java +++ b/desktop/src/main/java/bisq/desktop/main/market/offerbook/OfferBookChartViewModel.java @@ -280,18 +280,7 @@ private void updateChartData() { .map(OfferBookListItem::getOffer) .filter(e -> e.getCurrencyCode().equals(selectedTradeCurrencyProperty.get().getCode()) && e.getDirection().equals(OfferPayload.Direction.BUY)) - .sorted((o1, o2) -> { - long a = o1.getPrice() != null ? o1.getPrice().getValue() : 0; - long b = o2.getPrice() != null ? o2.getPrice().getValue() : 0; - if (a != b) { - if (CurrencyUtil.isCryptoCurrency(o1.getCurrencyCode())) - return a > b ? 1 : -1; - else - return a < b ? 1 : -1; - } else { - return 0; - } - }) + .sorted(getComparatorWithSecondarySortOrder(false)) .collect(Collectors.toList()); final Optional highestBuyPriceOffer = allBuyOffers.stream() @@ -320,18 +309,7 @@ private void updateChartData() { .map(OfferBookListItem::getOffer) .filter(e -> e.getCurrencyCode().equals(selectedTradeCurrencyProperty.get().getCode()) && e.getDirection().equals(OfferPayload.Direction.SELL)) - .sorted((o1, o2) -> { - long a = o1.getPrice() != null ? o1.getPrice().getValue() : 0; - long b = o2.getPrice() != null ? o2.getPrice().getValue() : 0; - if (a != b) { - if (CurrencyUtil.isCryptoCurrency(o1.getCurrencyCode())) - return a < b ? 1 : -1; - else - return a > b ? 1 : -1; - } else { - return 0; - } - }) + .sorted(getComparatorWithSecondarySortOrder(true)) .collect(Collectors.toList()); final Optional highestSellPriceOffer = allSellOffers.stream() @@ -355,6 +333,41 @@ private void updateChartData() { buildChartAndTableEntries(allSellOffers, OfferPayload.Direction.SELL, sellData, topSellOfferList); } + /** + * Returns a comparator to be used for Offers. Sorts primarily for price and + * secondarily for offer volume. + * + * @param reversePrimarySortOrder + * @return + */ + private Comparator getComparatorWithSecondarySortOrder(boolean reversePrimarySortOrder) { + Comparator primary = Comparator.comparing(Offer::getPrice, (o1, o2) -> { + long a = o1 != null ? o1.getValue() : 0; + long b = o2 != null ? o2.getValue() : 0; + if (a != b) { + if (o1 != null && CurrencyUtil.isCryptoCurrency(o1.getCurrencyCode())) + return a > b ? 1 : -1; + else + return a < b ? 1 : -1; + } else { + return 0; + } + }); + + // 1st comparator: sort by price + // - ascending, if showing offers to buyers + // - descending, if showing offers to sellers + if (reversePrimarySortOrder) + primary = primary.reversed(); + + // 2nd comparator: sort by amount, in descending order + // The goal is to show the more attractive offers at the top + // Both buyers and sellers would prefer to see higher amounts first (for offers with the same price) + Comparator secondary = Comparator.comparing(Offer::getAmount, Comparator.reverseOrder()); + + return primary.thenComparing(secondary); + } + private void buildChartAndTableEntries(List sortedList, OfferPayload.Direction direction, List> data,