Using the Collections.sort () Method to Sort Objects Alphabetically

I tried using Collections.sort(shapes) , but he gave me this error:

 Bound mismatch: The generic method sort(List<T>) of type Collections is not applicable for the arguments (ArrayList<CreateShape>). The inferred type CreateShape is not a valid substitute for the bounded parameter <T extends Comparable<? super T>> 

How do I fix this?

Class CreateSpace

  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 an error here } spaceWithShapes = space; shapes.add((CreateShape)shape); Collections.sort(shapes); // <<------- getting an error here } } 
+5
source share
1 answer

You got an error because when you call Collections.sort() , passing only the List<T> as parameter, it expects the list items to implement the Comparable interface. Since this is not the case with CreateShape , sort() has no way of knowing how these objects should be sorted.

Here are two options you should consider:

  • CreateShape can implement Comparable<CreateShape> : do this if you think that CreateShape instances have a natural order in which they should be sorted. If you want to sort by char field, for 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 you can just call Collections.sort() :

 Collections.sort(shapes); 
  1. Create a custom Comparator<CreateShape> : do this if you want to sort CreateShape instances arbitrarily. You may have a Comparator that sorts by name, another that sorts by 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 you should call Collections.sort() , passing the comparator as a parameter:

 Collections.sort(shapes, DisplayCharComparator.INSTANCE); 

Note. I implemented DisplayCharComparator as a single line. This is because it has no state, so there is no need to have more than one instance of this comparator. An alternative is to use a static variable:

 class CreateShape { static final Comparator<CreateShape> DISPLAY_CHAR_COMPARATOR = new DisplayCharComparator(); static class DisplayCharComparator implements Comparator<CreateShape> { ... } // rest of the code } 

Or, if you are using Java 8, you can use Comparator.comparing :

 shapes.sort(Comparator.comparing(CreateShape::getDisplayChar)); 
+2
source

All Articles