ordering vector

Sorry, but I have a question concerning the sort function. I tried to read the references, but I did not find the answer for my question. In this piece of code
[
#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>

using namespace std;

struct persons{
string firstname;
string lastname;
string date;
};

int main(){
vector<persons> people;
}
]

I want to order the persons objects within the vector according to the last name. I instead of persons my people vector was a container of int's, I would just do sort(people.begin(), people.end()). But, as I have persons, how do I do this?

I already tried to write something like:
sort(people.begin(), people.end(), lastname), but the compiler just told me lastname was undeclared.
Whenever you have problem with the standard library, refer to the reference on this site:
http://www.cplusplus.com/reference/algorithm/sort/

1
2
3
4
5
6
7
8
bool person_cmp_lt_lastname(const persons& p1,const persons& p2)
{
  return p1.lastname<p2.lastname;
}

[...]

sort(people.begin(), people.end(),person_cmp_lt_lastname);


By the way, persons should probably be named person, as the class defines just a single person.
Topic archived. No new replies allowed.