0

If I have a ArrayList of Columns and Columns have the field disparityRating which is an int how do I sort the arraylist of column by the value of the disparityRating field?

I have tried implementing the comparable interface on my column class but when I do and change the compare to parameter to Column like this:

@Override
public int compareTo(Column o) {
    return 0;
}

It comes up with an error saying method does not override method from its superclass

My main class has an arraylist of columns

Collection<Column> columns = new ArrayList<>();

and so I want to be able to sort them by using

Collections.sort(columns);
Moulie415
  • 317
  • 3
  • 13

2 Answers2

2

Add "implements Comparable".

public class ... extends ... implements Comparable<Column> {

    @Override
    public int compareTo(Column o) {
       return Integer.compare(disparityRating, o.disparityRating);
    }
}
Dan Armstrong
  • 469
  • 2
  • 5
1

Or you could :

 Collections.sort(ListOfCols, new Comparator<Column>() {
                    @Override
                    public int compare(Column o1,Column o2) {
                       return o2.getRating().compareTo(o1.getRating());
                        }
                    });
Joey
  • 203
  • 1
  • 11