Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- public static void main(String[] args) throws Exception {
- MyEnum[] e = new MyEnum[] { A, C, D, B }; // 1, 3, 4, 2
- Arrays.sort(e, new EnumComparator());
- System.out.println(Arrays.toString(e)); // [1, 2, 3, 4]
- }
- }
- class EnumComparator implements Comparator<MyEnum> {
- @Override
- public int compare(MyEnum o1, MyEnum o2) {
- return o1.toString().compareTo(o2.toString());
- }
- }
- enum MyEnum {
- D("4"),
- A("1"),
- B("2"),
- C("3");
- private String index;
- private MyEnum(String index) {
- this.index = index;
- }
- @Override
- public String toString() {
- return index;
- }
- }
Add Comment
Please, Sign In to add comment