How to sort a struct??

My assignment is to create a structs and sort all the employees by last name, first name, id number and salary. I am confused on how to compare all of their last names in a function. If someone can help or provide a hint, that'd be helpful!
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
 #include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;
struct date
{
    int year;
    int month;
    int day;
};
struct employee
{
    string firstname;
    string lastname;
    int idNum;
    float salary;
};
ostream& operator<< (ostream& out, employee e)
{
    out << e.idNum << ' ' << e.lastname
        << ' ' << e.firstname << ' '
        << e.salary;
    return out;
}

int main()
{
    employee frank;
    frank.idNum = 29837;
    frank.lastname = "Barton";
    frank.firstname = "Frank";
    frank.salary = 43560;
    cout << frank.idNum  << " " << frank.lastname <<setw(4) << "" << frank.firstname << setw(3) << "" << frank.salary<< endl;

    employee kim;
    kim.idNum = 29999;
    kim.lastname = "Cline";
    kim.firstname = "Kim";
    kim.salary = 1038094;
    cout << kim.idNum << " " << kim.lastname << setw(5) << "" << kim.firstname << setw(5) << "" <<setprecision(7) << kim.salary << endl;

    employee alice;
    alice.idNum = 29140;
    alice.lastname = "Zarkov";
    alice.firstname = "Alice";
    alice.salary = 87100;
    cout << alice.idNum << " " << alice.lastname << setw(4) << "" << alice.firstname << setw(3) << "" << alice.salary << endl;

    employee zoltan;
    zoltan.idNum = 29570;
    zoltan.lastname = "Middleton";
    zoltan.firstname = "Zoltan";
    zoltan.salary = 931352;
    cout << zoltan.idNum << " " << zoltan.lastname << " " << zoltan.firstname << setw(2) << "" <<  zoltan.salary << endl;
}

Hint:
the code above kind like
int x=5;
int y=6;
int z=2;
int w=4;

Meaning the struct are just variables. Can we sort variables in such way? No, why? because, it's just messy.

So what is the better way for x, y, z, w ? Array, just sort in the a loop.

The hint for your project is array of structs? Unless there are other requirements in the project.


closed account (48T7M4Gy)
I have a faint suspicion these might be useful, a key word is 'comparator' :)

http://stackoverflow.com/questions/3882467/defining-operator-for-a-struct
http://stackoverflow.com/questions/16093413/comparison-operator-overloading-for-a-struct-symmetrically-comparing-my-struct
http://www.cplusplus.com/reference/algorithm/sort/

The example in there does not have the new option of C++11: lambda syntax, so here:
1
2
3
4
std::sort( myvector.begin(),
           myvector.end(),
           []( const employee & lhs, const employee & rhs ) { return lhs.lastname < rhs.lastname; }
         );
Can I ask what lhs & hrs stand for?
Variable, function, and (custom) type names do not need to "stand for" anything, like in
1
2
3
4
5
for ( int i=0 i < 42; ++i ) {
  for ( int j=0 j < 7; ++j ) {
    // something
  }
}

the 'i' and 'j' do not reveal what the loops iterate over (which is of course questionable coding style).


Nevertheless, when you have a binary operator, such as <, in use:
foo < bar
the 'foo' is on the left-hand side of < and
the 'bar' is on the right-hand side of <

In the example of std::sort there were:
bool myfunction (int i, int j) { return (i<j); }
Would it be more or less clear, if the variables had been renamed like this:
bool myfunction (int lhs, int rhs) { return (lhs<rhs); }
this is my code. does anyone know to put commas in the salary like 1000 is 1,000 ?
1
2
3
4
5
6
7
8
void print(vector<employee>& team) {
  for(int i = 0; i < team.size(); i++) {
    cout << team.at(i).idNum << " " << right << setw(10) << team.at(i).lastname << right <<
    setw(8) << team.at(i).firstname << setw(5) << right << '$' << setprecision(7) <<
    team.at(i).salary << " " << setw(15) << right << team.at(i).hireDate << endl;
  }
  cout << endl << endl;
}
closed account (48T7M4Gy)
http://stackoverflow.com/questions/17530408/print-integer-with-thousands-and-millions-separator
http://stackoverflow.com/questions/27767781/why-was-the-space-character-not-chosen-for-c14-digit-separators
Last edited on
Try the example shown here:

http://en.cppreference.com/w/cpp/locale/money_put

Good Luck !!
Topic archived. No new replies allowed.