Sorting by the Numerical Value
When you sort by the numerical value, the currency will be ignored and only the numerical value will be used in the comparisons. Note that this sorting will not factor in exchange rates. In other words, ten Brazilian Real will be considered equal to ten US Dollars. Sorting can be in ascending or descending order. Here's an example.
public class SortMonetaryAmountNumber {
public static void main(String[] args) {
CurrencyUnit dollar = Monetary.getCurrency("USD");
CurrencyUnit euro = Monetary.getCurrency("EUR");
CurrencyUnit real = Monetary.getCurrency("BRL");
MonetaryAmount money = Money.of(9, euro);
MonetaryAmount money2 = Money.of(10, dollar);
MonetaryAmount money3 = Money.of(11, real);
List<MonetaryAmount> resultAsc = Stream.of(money, money2, money3)
.sorted(MonetaryFunctions
.sortNumber()).collect(Collectors.toList());//[EUR 9, USD 10, BRL 11]
List<MonetaryAmount> resultDesc = Stream.of(money, money2, money3)
.sorted(MonetaryFunctions
.sortNumberDesc()).collect(Collectors.toList());//[BRL 11, USD 10, EUR 9]
}
}