Alphabetize specific data

I'm new to C++ and I need advice as to how I go about doing this...

I have a data file with the following format.
FIRSTNAME CLASTNAME INTVALUE INTVALUE
FIRSTNAME ALASTNAME INTVALUE INTVALUE
FIRSTNAME BLASTNAME INTVALUE INTVALUE

What I'm trying to do...
ALASTNAME FIRSTNAME INTVALUE INTVALUE
BLASTNAME FIRSTNAME INTVALUE INTVALUE
CLASTNAME FIRSTNAME INTVALUE INTVALUE

How would I go about sorting it by last name while still retaining the data that belongs to each person? So far I've only been able to sort the last name alone but that doesn't help because I don't have to rest of the data...
Last edited on
By example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# include <iostream>
# include <algorithm>
# include <tuple>
# include <vector>

struct Person { 
    std::string first, last;    
    int a, b;
};

bool operator<(Person const& a, Person const& b) {
    return std::tie(a.last, a.first, a.a, a.b) < 
           std::tie(b.last, b.first, b.a, b.b);
} 
std::ostream& operator<<(std::ostream& s, Person const& a) 
{ return s << a.first << ' ' << a.last << ' ' << a.a << ' ' << a.b; }
std::istream& operator>>(std::istream& s, Person& p)
{ return s >> p.first >> p.last >> p.a >> p.b; }

int main() { 
    std::vector<Person> v;
    for (Person foo; std::cin >> foo;) v.push_back(std::move(foo));
    std::sort(v.begin(), v.end()); 
    for (auto const& elt: v) std::cout << elt << '\n';
}

Demo:
http://coliru.stacked-crooked.com/a/a564aa9977e1addb
Last edited on
Topic archived. No new replies allowed.