0

I want to return the top k ratables from the ArrayList based on the Comparator "c" in order . so the first element is the first ratable according to the comparator c.

this is my code

public static ArrayList<Ratable> getTopK(ArrayList<Ratable> list, int k, Comparator c) {
        ArrayList<Ratable> ratables;

        if (c.toString().contains("AlphabeticalComparator")) {
            ratables = AlphabeticalComparator.compare(list);
        } else if (c.toString().contains("NumberOfRatingsComparator")) {
            ratables = NumberOfRatingsComparator.compare(list);
        } else {
            Scanner in = new Scanner(System.in);
            System.out.println("Enter Extra Reviews and value of extra ratings: ");
            int extraReviews = in.nextInt();
            double extraRatings = in.nextDouble();
            ratables = BayesianAverageComparator.compare(list, extraReviews, extraRatings);
        }

        ArrayList<Ratable> subList = new ArrayList<Ratable>(ratables.subList(0, k));
        return subList;
    }

    }

My autograder is telling me it is not ordered the way it needs to be what can I do?

jhonathan
  • 21
  • 2

1 Answers1

-1

You can use Collections.sort(list, comparator).

QuentinC
  • 12,311
  • 4
  • 24
  • 37