java - Sort enumeration values by name -
i have enumeration values follows.
public enum types { day, night, hello, age, sum; }
anyone please help me sort name when take values controller like
types.values());
thanks.
call arrays#sort
comparator
types
:
types[] arr = types.values(); arrays.sort(arr, new comparator<types>() { @override public int compare(types o1, types o2) { homecoming o1.name().compareto(o2.name()); } }); // arr [age, day, hello, night, sum]
an alternate (but less maintainable) solution declare enum constants in alphabetical order:
public enum types { age, day, hello, night, sum }
java
No comments:
Post a Comment