Reputation: 615
I have a enum for Indian currencies.
public enum HardCurrency {
ONE(1), FIVE(5), TEN(10), TWENTY(20), FIFTY(50), HUNDRED(100), FIVE_HUNDRED(
500), TWO_THOUSAND(2000);
final static Logger logger = LoggerFactory.getLogger(HardCurrency.class);
private final BigDecimal value;
HardCurrency(int value) {
this.value = new BigDecimal(value);
}
public BigDecimal getValue() {
return value;
}
}
If I iterate over all the values of this enum, this is how the code looks like
for (HardCurrency curr : HardCurrency.values()) {
logger.debug("{}", curr.getValue());
}
And this gives the following output
[main] DEBUG c.p.v.ListAllValuesOfEnum - 1
[main] DEBUG c.p.v.ListAllValuesOfEnum - 5
[main] DEBUG c.p.v.ListAllValuesOfEnum - 10
[main] DEBUG c.p.v.ListAllValuesOfEnum - 20
[main] DEBUG c.p.v.ListAllValuesOfEnum - 50
[main] DEBUG c.p.v.ListAllValuesOfEnum - 100
[main] DEBUG c.p.v.ListAllValuesOfEnum - 500
[main] DEBUG c.p.v.ListAllValuesOfEnum - 2000
I need to get it the other way round and have them iterated in decreasing value.
What is the best way of listing all the values of an enum in a guaranteed order.
Upvotes: 1
Views: 1167
Reputation: 3455
The ordering of the array depends on the order defined in source code. But you should not rely on that, because it may change in the future, or because of conflicting use-cases that expect a different order.
The cleanest solution to guarantee descending order is to explicitly sort by value. In order to sort, you need to supply a Comparator
. The Java 8 way of defining such a Comparator is a one-liner:
Comparator.comparing(HardCurrency::getValue).reversed()
Upvotes: 2
Reputation: 543
I've tested below code and works properly:
Comparator comparator1 = new Comparator<HardCurrency>() {
public int compare(HardCurrency e1, HardCurrency e2) {
if (e1.getValue().intValue() > e2.getValue().intValue()) {
return -1;
} else if (e1.getValue().intValue() < e2.getValue().intValue()) {
return 1;
}
return 0;
}
};
HardCurrency[] allCurrency = HardCurrency.values();
Arrays.sort(allCurrency,comparator1);
for (HardCurrency curr : allCurrency) {
System.out.println(curr.getValue());
}
Upvotes: -2
Reputation: 497
I think that the best way is to create Comparator
implementation and use it to sort values.
You can define inverse comparator as follows:
import java.math.BigDecimal;
import java.util.Comparator;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class MyClass {
public static void main(String args[]) {
List<HardCurrency> hardCurrencies = Arrays.asList(HardCurrency.values());
Collections.sort(hardCurrencies, new HardCurrencyInverseComparator());
for (HardCurrency hc : hardCurrencies) {
System.out.println(hc.getValue());
}
}
enum HardCurrency {
ONE(1), FIVE(5), TEN(10), TWENTY(20), FIFTY(50), HUNDRED(100), FIVE_HUNDRED(
500), TWO_THOUSAND(2000);
private final BigDecimal value;
HardCurrency(int value) {
this.value = new BigDecimal(value);
}
public BigDecimal getValue() {
return value;
}
}
static class HardCurrencyInverseComparator implements Comparator<HardCurrency> {
public int compare(HardCurrency currency1, HardCurrency currency2) {
return currency2.getValue().compareTo(currency1.getValue());
}
}
}
Upvotes: 2