I thought that the values() method would give me an ordered view of the enum (as mentioned here), but this is not the case here. I simply get the enum members in the order I created them within the Letter enum class.
Precisely, the order of declaration is considered significant for enums, so we are glad that they are returned in precisely that order. For example, when a int i
represents an enum values, doing values()[i]
is a very simple and efficient way to find the enum instance. To go contrary-wise, the ordinal()
method returns the index of an enum instance.
Is there a way to output the values of an enum in alphabetical order? Would I need a separate comparator object, or is there a built-in way to do this? Basically I would like the values to be alphabetically sorted based on the getDescription() text:
What you call value is not something defined for enums in general. Here, in your context, you mean the result of getDescription()
.
As you say, you could create a Comparator for these descriptions. That would be perfect :-)
Note that in general, you could need several orders for these instances:
- declaration order (this is the official order)
- description order
- others as needed
You could also push that notion of DescriptionComparator a little bit:
For performance reasons, you could store the computed descriptions.
Because enums can't inherit, code reuse has to be outside the enum class. Let me give the example we would use in our projects:
Now the code samples...
/** Interface for enums that have a description. */
public interface Described {
/** Returns the description. */
String getDescription();
}
public enum Letter implements Described {
// .... implementation as in the original post,
// as the method is already implemented
}
public enum Other implements Described {
// .... same
}
/** Utilities for enums. */
public abstract class EnumUtils {
/** Reusable Comparator instance for Described objects. */
public static Comparator<Described> DESCRIPTION_COMPARATOR =
new Comparator<Described>() {
public int compareTo(Described a, Described b) {
return a.getDescription().compareTo(b.getDescription);
}
};
/** Return the sorted descriptions for the enum. */
public static <E extends Enum & Described> List<String>
getSortedDescriptions(Class<E> enumClass) {
List<String> descriptions = new ArrayList<String>();
for(E e : enumClass.getEnumConstants()) {
result.add(e.getDescription());
}
Collections.sort(descriptions);
return descriptions;
}
}
// caller code
List<String> letters = EnumUtils.getSortedDescriptions(Letter.class);
List<String> others = EnumUtils.getSortedDescriptions(Other.class);
Note that the generic code in EnumUtils
works not only for one enum class, but works for any enum class in your project that implements the Described
interface.
As said before, the point of having the code outside of the enums (where it would otherwise belong) is to reuse the code. It's not big deal for two enums, but we have over a thousand enums in our project, many of them with the same interfaces...!