fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. private static enum Type {
  11. A, B, C
  12. }
  13.  
  14. private static class Friendship implements Comparable<Friendship> {
  15. @Override
  16. public String toString() {
  17. return "[" + username + " type:" + type + "]";
  18. }
  19.  
  20. public Friendship(Type type, String username) {
  21. super();
  22. this.type = type;
  23. this.username = username;
  24. }
  25.  
  26. Type type;
  27.  
  28. public Type getType() {
  29. return type;
  30. }
  31.  
  32. String username;
  33.  
  34. public String getUsername() {
  35. return username;
  36. }
  37.  
  38. public int compareTo(Friendship another) {
  39.  
  40. if (!this.getType().equals(another.getType())) {
  41. // if type are not equal, so we might have at most one A
  42.  
  43. if (this.getType().equals(Type.A)) { // on left side
  44. return -1;
  45. }
  46.  
  47. if (another.getType().equals(Type.A)) { // or, on rightside
  48. return 1;
  49. }
  50. }
  51. // or we have on both sides or neither side
  52. return another.getUsername().compareTo(this.getUsername());
  53. }
  54.  
  55. }
  56.  
  57. public static void main(String[] args) {
  58. List<Friendship> list = Arrays.asList(new Friendship(Type.C, "aa"),
  59. new Friendship(Type.A, "cc"), new Friendship(Type.B, "bb"),
  60. new Friendship(Type.B, "aa"), new Friendship(Type.C, "cc"),
  61. new Friendship(Type.C, "zz"), new Friendship(Type.A, "bb"));
  62. Collections.sort(list);
  63. for (Friendship item : list)
  64. System.out.println(item);
  65. }
  66.  
  67. }
Success #stdin #stdout 0.1s 320320KB
stdin
Standard input is empty
stdout
[cc type:A]
[bb type:A]
[zz type:C]
[cc type:C]
[bb type:B]
[aa type:C]
[aa type:B]