Sorting by Currency
When you sort by currency, Moneta sorts the list based on the lexicographical order of the currency code. For example, a list of money with the currencies USD
, EUR
, BRL
will be sorted to BRL
, EUR
and USD
when sorted in ascending order or USD
, EUR
, BRL
when sorted in descending order. Here's an example.
public class SortMonetaryAmountCurrency {
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
.sortCurrencyUnit()).collect(Collectors.toList());//[BRL 11, EUR 9, USD 10]
List<MonetaryAmount> resultDesc = Stream.of(money, money2, money3)
.sorted(MonetaryFunctions
.sortCurrencyUnitDesc()).collect(Collectors.toList());//[USD 10, EUR 9, BRL 11]
}
}