Guest User

Untitled

a guest
Jan 23rd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. public class Main {
  2.  
  3. public static void main(String[] args) throws Exception {
  4.  
  5. MyEnum[] e = new MyEnum[] { A, C, D, B }; // 1, 3, 4, 2
  6. Arrays.sort(e, new EnumComparator());
  7. System.out.println(Arrays.toString(e)); // [1, 2, 3, 4]
  8.  
  9. }
  10. }
  11.  
  12. class EnumComparator implements Comparator<MyEnum> {
  13.  
  14. @Override
  15. public int compare(MyEnum o1, MyEnum o2) {
  16. return o1.toString().compareTo(o2.toString());
  17. }
  18.  
  19. }
  20.  
  21. enum MyEnum {
  22.  
  23. D("4"),
  24. A("1"),
  25. B("2"),
  26. C("3");
  27.  
  28. private String index;
  29.  
  30. private MyEnum(String index) {
  31. this.index = index;
  32. }
  33.  
  34. @Override
  35. public String toString() {
  36. return index;
  37. }
  38.  
  39. }
Add Comment
Please, Sign In to add comment