user147 user147 - 14 days ago 7
C++ Question

C++ Sorting vector of structs alphabetically

I have this record management program I am building for an introductory programming course. Part of the code is the function

sortEmail
which is supposed to sort the vector recordList alphabetically and calls the
sortAlphabet
function. I'm getting an error on this line:
sort(recordList.begin(), recordList.end(), compareAlphabet);
that "sort was not declared in this scope." How can I fix this?

#include<iostream>
#include<vector>
#include<string>
using namespace std;
struct Record
{
string name;
string email;
};

bool compareAlphabet(const Record& a, const Record& b)
{
return a.name < b.name;
}

sortEmail(vector<Record>& recordList)
{
system("cls");
sort(recordList.begin(), recordList.end(), compareAlphabet);
cout<<"Name have been sorted alphabetically"<<endl;
for (int i=0; i!=recordList.size(); i++)
{
cout<<recordList[i].name<<endl;
cout<<recordList[i].email<<endl;
}
system("PAUSE");
system("cls");
}

Answer

I think you forgot to include <algorithm>
Add #include <algorithm> and see if it works.