0

i have a vector that stores students as objects each student object has data containing grades that are input by the user, the user enters 4 grades and gets a total grade. i want to be able to sort this data in a list going from highest to lowest but i am having trouble figuring it out, this is what i have for the function so far however it does not run it returns the error that cout doesnt match operands. im sure there is a far better way to this, any help would be greatly appreciated!

    static void compare(vector<Student>& students) {
    vector<Student> students{};

    sort(students.begin(), students.end());

    cout << "Sorted \n";
    for (auto x : students) {
        cout << x << " ";
    }

}

1 Answers1

2

The compiler doesn't know how to compare students. There are several options. First, you may define a comparison operator:

bool operator < (const Student&, const Student&);

Next, you may define a comparison operation just for one call of your sort function:

sort(students.begin(), students.end(), [](auto &a, auto &b) { return ...; });
Dmitry Kuzminov
  • 6,180
  • 6
  • 18
  • 40