java - Using Collections.sort() method to sort objects alphabetically -


i've tried use collections.sort(shapes) it's giving me error:

bound mismatch: generic method sort(list<t>) of type collections not applicable   arguments (arraylist<createshape>). inferred type createshape not valid substitute   bounded parameter <t extends comparable<? super t>> 

how should fix that?

createspace class

 public class createspace implements space{              private int height;         private int width;         private string layout;         private char[][] space;         private shape originalshape;         private arraylist<createshape> shapes = new arraylist<createshape>();          public createspace(int height, int width, char[][] space, string layout)         {             this.height = height;             this.width = width;             this.space = space;             this.layout = layout;         }     public void placeshapeat(int row, int col, shape shape)         {              int sheight = shape.getheight();             int swidth = shape.getwidth();             if(shape.getheight() + row > space.length || shape.getwidth() + col > space[0].length)                 throw new fititexception("out of bounds!");             char [][] spacewithshapes = space;             if(shapefitsat(row, col, shape) == true)             {                 for(int r = 0; r < sheight; r++)                 {                     for(int c = 0; c < swidth; c++)                     {                          if(spacewithshapes[r+row][c+col] == '.' && shape.isfilledat(r, c) == false)                             spacewithshapes[r+row][c+col] = (((createshape)shape).getshape()[r][c]);                     }                 //  shapes.add((createshape)shape);                     collections.sort(shapes); // <<------- getting error here                 }                 spacewithshapes = space;                 shapes.add((createshape)shape); collections.sort(shapes); // <<------- getting error here             }         } 

you got error because when call collections.sort() passing list<t> parameter, expects list elements implement comparable interface. since not case createshape, sort() has no way know how these objects should sorted.

here 2 options should consider:

  1. createshape implement comparable<createshape>: if think createshape instances have natural order in should sorted. if wanted sort char field, example:

    class createshape implements comparable<createshape> {      private char displaychar;       public char getdisplaychar() {          return displaychar;       }       @override      public int compareto(createshape that) {          return character.compare(this.displaychar, that.displaychar);      }  } 

then call collections.sort():

collections.sort(shapes); 
  1. create custom comparator<createshape>: if want sort createshape instances arbitrarily. have comparator sorts name, sorts id, etc. example:

    enum displaycharcomparator implements comparator<createshape> {     instance;      @override     public int compare(createshape s1, createshape s2) {         return character.compare(s1.getdisplaychar(), s2.getdisplaychar());     } } 

then should call collections.sort() passing comparator parameter:

collections.sort(shapes, displaycharcomparator.instance); 

note implemented displaycharcomparator singleton. that's because has no state, there no need have more 1 instance of comparator. alternative use static variable:

class createshape {      static final comparator<createshape> display_char_comparator =         new displaycharcomparator();      static class displaycharcomparator implements comparator<createshape> { ... }      // rest of code } 

or, if you're using java 8, can use comparator.comparing:

shapes.sort(comparator.comparing(createshape::getdisplaychar));