使用Collections.sort()方法按字母顺序对对象进行排序

时间:2015-04-27 20:31:04

标签: java

我尝试使用Collections.sort(shapes),但它给了我这个错误:

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>>

我该如何解决这个问题?

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
            }
        }

1 个答案:

答案 0 :(得分:2)

您收到错误,因为当您调用Collections.sort()仅传递List<T>作为参数时,它会期望列表元素实现Comparable接口。由于CreateShape不是这种情况,sort()无法知道应如何对这些对象进行排序。

您应该考虑以下两个选项:

  1. CreateShape可以实现Comparable<CreateShape>:如果您认为CreateShape个实例具有应对其进行排序的自然顺序,请执行此操作。如果您想按char字段排序,例如:

    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);
         }
     }
    
  2. 然后你可以简单地拨打Collections.sort()

    Collections.sort(shapes);
    
    1. 创建自定义Comparator<CreateShape>:如果要对CreateShape个实例进行任意排序,请执行此操作。您可以按名称排序Comparator,按ID排序的另一个排序等。示例:

      enum DisplayCharComparator implements Comparator<CreateShape> {
          INSTANCE;
      
          @Override
          public int compare(CreateShape s1, CreateShape s2) {
              return Character.compare(s1.getDisplayChar(), s2.getDisplayChar());
          }
      }
      
    2. 然后你应该调用Collections.sort()将比较器作为参数传递:

      Collections.sort(shapes, DisplayCharComparator.INSTANCE);
      

      注意我将DisplayCharComparator实现为单身人士。那是因为它没有状态,所以不需要有这个比较器的多个实例。另一种方法是使用静态变量:

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

      或者,如果您使用的是Java 8,则可以使用Comparator.comparing

      shapes.sort(Comparator.comparing(CreateShape::getDisplayChar));