fork download
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3.  
  4. interface bc {}
  5.  
  6. class b implements bc, Comparable<bc> {
  7. String str1;
  8.  
  9. b (String var) {
  10. str1 = var;
  11. }
  12.  
  13. public int compareTo(bc object) {
  14. if(object instanceof b)
  15. return str1.compareTo(((b)object).str1);
  16.  
  17. if(object instanceof c)
  18. return str1.compareTo(((c)object).str2);
  19.  
  20. return 0;
  21. }
  22. }
  23.  
  24. class c implements bc, Comparable<bc> {
  25. String str2;
  26.  
  27. c (String var) {
  28. str2 = var;
  29. }
  30.  
  31. public int compareTo(bc object) {
  32. if(object instanceof b)
  33. return str2.compareTo(((b)object).str1);
  34.  
  35. if(object instanceof c)
  36. return str2.compareTo(((c)object).str2);
  37.  
  38. return 0;
  39. }
  40. }
  41.  
  42. public class Main {
  43. public static void main(String[] args) {
  44. b obj1 = new b("aman");
  45. c obj2 = new c("monster");
  46. b obj3 = new b("back");
  47. c obj4 = new c("a");
  48.  
  49. ArrayList<bc> list = new ArrayList<bc>();
  50.  
  51. list.add(obj1);
  52. list.add(obj2);
  53. list.add(obj3);
  54. list.add(obj4);
  55.  
  56. System.out.println("unsorted list = "+list);
  57. Collections.sort(list);
  58. System.out.println("sorted list = "+list);
  59. }
  60. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:57: error: no suitable method found for sort(ArrayList<bc>)
		Collections.sort(list);
		           ^
    method Collections.<T#1>sort(List<T#1>,Comparator<? super T#1>) is not applicable
      (cannot instantiate from arguments because actual and formal argument lists differ in length)
    method Collections.<T#2>sort(List<T#2>) is not applicable
      (inferred type does not conform to declared bound(s)
        inferred: bc
        bound(s): Comparable<? super bc>)
  where T#1,T#2 are type-variables:
    T#1 extends Object declared in method <T#1>sort(List<T#1>,Comparator<? super T#1>)
    T#2 extends Comparable<? super T#2> declared in method <T#2>sort(List<T#2>)
1 error
stdout
Standard output is empty