Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Secondary sort order in Market Offer Lists #4168

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<Offer> highestBuyPriceOffer = allBuyOffers.stream()
Expand Down Expand Up @@ -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<Offer> highestSellPriceOffer = allSellOffers.stream()
Expand All @@ -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<Offer> getComparatorWithSecondarySortOrder(boolean reversePrimarySortOrder) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this name is preferable: getOfferComparator(...). The problem with getComparator was just that it didn't mention the object of comparison.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not requesting this change, since the whole method should be unnecessary.

Comparator<Offer> 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<Offer> secondary = Comparator.comparing(Offer::getAmount, Comparator.reverseOrder());

return primary.thenComparing(secondary);
}

private void buildChartAndTableEntries(List<Offer> sortedList,
OfferPayload.Direction direction,
List<XYChart.Data<Number, Number>> data,
Expand Down