Click here to Skip to main content
14,974,631 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
the class(staff) contains objects int ID, string Name, string Class
The vector contains
vector <staff> s = { {234, "Mark", "biology"},
{3455, "Mitch", "English"},
{1234, "Hen", "Maths"}}

How can I sort this from ID? and print sorted?
Thank you

What I have tried:

I haven't tried anything just stuck at this part.
Updated 9-Apr-21 3:32am

1 solution

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
  struct staff
    int id;
    string name;
    string cls;

  vector <staff> s = {
  {234, "Mark", "Biology"},
  {3455, "Mitch", "English"},
  {1234, "Hen", "Maths"}

  sort( s.begin(), s.end(), [](const staff &a, const staff &b){ return ( <;});

  for (const auto  & x : s)
    cout << << ", " << << ", " << x.cls << endl;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900