Learning about "custom comparators" will solve your problem. Here is one of the ways to achieve the desired result. I am using cmp as a custom comparator. Notice that it is passed while calling sort().
This way is useful if you need to sort on basis of something else tomorrow, say phoneNumber. The only change you would need is adding or updating a comparator function to return c1.phoneNumber < c2.phoneNumber;
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef struct contacts
{
string name;
string nickName;
string phoneNumber;
string carrier;
string address;
//callDetails callDetails;
} contactDetails;
//sort based on name
bool cmp (const contacts& c1, const contacts& c2) {
return c1.name < c2.name;
}
void print(vector <contactDetails> proContactFile) {
for (auto s : proContactFile) {
cout << s.name << " " << s.nickName << " " << s.phoneNumber << " " << s.carrier << " " << s.address << endl;
}
}
int main()
{
vector <contactDetails> proContactFile;
proContactFile.push_back({"name1","nickName1","phone1","carrier1", "address1"});
proContactFile.push_back({ "ame1","ickName1","hone1","arrier1", "ddress1" });
proContactFile.push_back({ "me1","ckName1","one1","rrier1", "dress1" });
proContactFile.push_back({ "e1","kName1","ne1","rier1", "ress1" });
sort(proContactFile.begin(), proContactFile.end(), cmp);
print(proContactFile);
}