This discussion is archived
5 Replies Latest reply: Jun 8, 2012 5:32 AM by 942513 RSS

Sort a List using Comparable and Generics -

AjayL Newbie
Currently Being Moderated
I am trying to sort a list using the Comparable Interface using Generics.
But I am getting the exception:
Bound Mismatch

How can I sort the list that I am passing to the sort() method?
abstract class Shape{
     
     private String shapeType;
     
public Shape(String shapeType){
          this.shapeType = shapeType;
     }
     
     abstract void displayWhoAmI();
     
     String getType(){
          return shapeType;
     }
     
}

class Circle extends Shape{

     public Circle(){
          super("Circle");
     }

     @Override
     void displayWhoAmI() {
          System.out.println("Circle");          
     }
}

class Rectangle extends Shape{

     public Rectangle() {
          super("Rectangle");
     }

     @Override
     void  displayWhoAmI() {
          System.out.println("Rectangle");
     }
}

public class WildCard_Test {

     public static void main(String[] args){
          
          List<Shape> shapeList = new ArrayList<Shape>();
          
          shapeList.add(new Rectangle());
          shapeList.add(new Circle());
     
          
          // Sort and Print the List
          sort(shapeList);
          
               
     // Sort the List
     // This forces the type T to have the compareTo(T other) method. This means you can do the following in your method:
     // E.Gif (a.compareTo(a[j]) > 0) } 
     static <T extends Comparable<? super T>> void sort(T list){
          
          
     }
     
     


     
}



  • 1. Re: Sort a List using Comparable and Generics -
    jtahlborn Expert
    Currently Being Moderated
    This bound "T extends Comparable<? super T>" indicates that the objects in the collection that you pass to the sort() method must implement "Comparable".
  • 2. Re: Sort a List using Comparable and Generics -
    AjayL Newbie
    Currently Being Moderated
    But supposing I have this design of Shape(being the abstract class) and is subclassed by Circle and Rectangle.
    So now my question is: If I create another class called Triangle,then I will be implementing the compareTo() for each and every subclass.

    So is my question wrong or my class design wrong?

    Thanks
  • 3. Re: Sort a List using Comparable and Generics -
    AjayL Newbie
    Currently Being Moderated
    I did the following and this works
    I hope this is the correct way.
    abstract class Shape implements Comparable<Shape>{
         
         private String shapeType;
         
         public Shape(String shapeType){
              this.shapeType = shapeType;
         }
         
         abstract void displayWhoAmI();
         
         String getType(){
              return shapeType;
         }
         
         @Override
         public int compareTo(Shape o) {
              return this.shapeType.compareTo(o.shapeType);
         }
    }
    
    class Circle extends Shape{
    
         public Circle(){
              super("Circle");
         }
    
         @Override
         void displayWhoAmI() {
              System.out.println("Circle in the Sand");          
         }
    }
    
    class Rectangle extends Shape{
    
         public Rectangle() {
              super("Rectangle");
         }
    
         @Override
         void  displayWhoAmI() {
              System.out.println("Rectangle in the Sand");
         }
    }
    
    public class WildCard_Test {
    
         public static void main(String[] args){
              
              List<Shape> shapeList = new ArrayList<Shape>();
              
              shapeList.add(new Rectangle());
              shapeList.add(new Circle());
         
              
              // Sort and Print the List
              sort(shapeList);
              
         
         }
    
         
         
         // Sort the List
         static /*<T extends Comparable<? super T>>*/ void sort(List<? extends Shape> list){
                        Collections.sort(list);
                         System.out.println(list);
         }
         
         
    }
  • 4. Re: Sort a List using Comparable and Generics -
    jtahlborn Expert
    Currently Being Moderated
    AjayL wrote:
    But supposing I have this design of Shape(being the abstract class) and is subclassed by Circle and Rectangle.
    So now my question is: If I create another class called Triangle,then I will be implementing the compareTo() for each and every subclass.

    So is my question wrong or my class design wrong?
    that depends on what you are trying to accomplish, which you haven't indicated. are you trying to make all of your object types mutually comparable, or just comparable within their various sub-types. which class implements Comparable and which class implements the compareTo() method is dependent on what you want your classes to do.
  • 5. Re: Sort a List using Comparable and Generics -
    942513 Newbie
    Currently Being Moderated
    You need to create your own comparator by implementing java.util.Comparator interface. Define its compare method as per your sorting requirements.
    When do sorting using Collections.sort() method, pass this comparator object. For example

    Collections.sort(listToBeSorted, new MyComparator());

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points