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.
Posted
Updated 9-Apr-21 3:32am
v2

1 solution

Try
C++
#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 (a.id < b.id);});

  for (const auto  & x : s)
    cout << x.id << ", " << x.name << ", " << 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