From 2b797dafedfcf7b133431a6ee06b9f8ae03d2992 Mon Sep 17 00:00:00 2001 From: Dominykas Mostauskis Date: Fri, 15 May 2020 19:15:17 +0200 Subject: [PATCH 1/2] Secondary sort order for offer book A rewrite of @freimair's PR https://github.com/bisq-network/bisq/pull/4168. Adds a secondary sort order of offers in market offer book by offer amount that goes from high to low. Also, refactors-away overcomplicated previous implementation of primary sort. Co-authored-by: Florian Reimair Co-authored-by: cd2357 <15956136+cd2357@users.noreply.github.com> --- .../offerbook/OfferBookChartViewModel.java | 39 +++++++------------ 1 file changed, 15 insertions(+), 24 deletions(-) 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..b1eaadd5e3f 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 @@ -276,22 +276,24 @@ private boolean isAnyPricePresent() { } private void updateChartData() { + + // Offer price can be null (if price feed unavailable), thus a null-tolerant comparator is used. + Comparator offerPriceComparator = Comparator.comparing(Offer::getPrice, Comparator.nullsLast(Comparator.naturalOrder())); + // Offer amounts are used for the secondary sort. They are sorted from high to low. + Comparator offerAmountComparator = Comparator.comparing(Offer::getAmount).reversed(); + + var buyOfferSortComparator = + offerPriceComparator.reversed() // Buy offers, as opposed to sell offers, are primarily sorted from high price to low. + .thenComparing(offerAmountComparator); + var sellOfferSortComparator = + offerPriceComparator + .thenComparing(offerAmountComparator); + List allBuyOffers = offerBookListItems.stream() .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(buyOfferSortComparator) .collect(Collectors.toList()); final Optional highestBuyPriceOffer = allBuyOffers.stream() @@ -320,18 +322,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(sellOfferSortComparator) .collect(Collectors.toList()); final Optional highestSellPriceOffer = allSellOffers.stream() From 3dc13b6effcd4dd08be113828da28a6d07d6a830 Mon Sep 17 00:00:00 2001 From: Dominykas Mostauskis Date: Mon, 25 May 2020 11:37:30 +0200 Subject: [PATCH 2/2] Fix reversed ordering in altcoin market --- .../main/market/offerbook/OfferBookChartViewModel.java | 8 ++++++++ 1 file changed, 8 insertions(+) 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 b1eaadd5e3f..dc6fcf4de36 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 @@ -279,6 +279,14 @@ private void updateChartData() { // Offer price can be null (if price feed unavailable), thus a null-tolerant comparator is used. Comparator offerPriceComparator = Comparator.comparing(Offer::getPrice, Comparator.nullsLast(Comparator.naturalOrder())); + + // Trading btc-fiat is considered as buying/selling BTC, but trading btc-altcoin is + // considered as buying/selling Altcoin. Because of this, when viewing a btc-altcoin pair, + // the buy column is actually the sell column and vice versa. To maintain the expected + // ordering, we have to reverse the price comparator. + boolean isCrypto = CurrencyUtil.isCryptoCurrency(getCurrencyCode()); + if (isCrypto) offerPriceComparator = offerPriceComparator.reversed(); + // Offer amounts are used for the secondary sort. They are sorted from high to low. Comparator offerAmountComparator = Comparator.comparing(Offer::getAmount).reversed();