How do I sort data structures by a field?

I'm writing a program that would allow the user to enter information for a database of 20 employees, and then sort the information by last name. Unfortunately, I don't know how to proceed with sorting the information. I was thinking of using code to bubble sort the information by last name, but I haven't done this with data structures with multiple fields. Any help would be greatly appreciated, I am posting the code so that anyone can see my program so far. If it helps any, I am using Visual C++ and codeblocks.

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//Database of 20 employees
//Will allow users to enter employee information
//information includes first and last name, id#, pay rate, hours, gross pay, federal tax, state tax, FICA, and Net Pay
//Must be able to sort by last name

#include <iostream>
#include <cctype>
#include <ctime>
#include <time.h>
#include <math.h>
#include <string>
#include <algorithm>
#include <windows.h>
#include "conio.h"
#include <stdio.h>
#include <iomanip> //input & output manipulation of files
#include <fstream> //filestream
#include <stdlib.h>
using namespace std;

struct totemdb{ string first, last, id; float payr, hours, gross, ftax, stax, fica, netp;}; //Record data-type
int empnum,emptot;
char choice;

totemdb empdatab[20];

int main()
{
		//===============Initialize Database
	empnum=0;

	cout<<"Now initializing the database with all zeroes.\n";
	system("PAUSE");

	while(empnum<20)
	{
		empdatab[empnum].id="00000";
		empdatab[empnum].first="First";
		empdatab[empnum].last="Last";
		empdatab[empnum].payr=0;
		empdatab[empnum].hours=0;
		empdatab[empnum].gross=0;
		empdatab[empnum].ftax=0;
		empdatab[empnum].stax=0;
		empdatab[empnum].fica=0;
		empdatab[empnum].netp=0;

		cout<<empdatab[empnum].id<<" "<<empdatab[empnum].first<<" "<<empdatab[empnum].last<<" "<<empdatab[empnum].payr<<" "<<empdatab[empnum].hours<<" "<<empdatab[empnum].gross<<" "<<empdatab[empnum].ftax<<" "<<empdatab[empnum].stax<<" "<<empdatab[empnum].fica<<" "<<empdatab[empnum].netp<<"\n";
		empnum++;
	}

	cout<<"The database is set. You will now enter the data for the individuals.\n";
	system("PAUSE");
	system("CLS");
	empnum=0;

	while(empnum<20)
	{
		cout<<"YOU ARE ENTERING INFORMATION FOR EMPLOYEE: "<<empnum<<"\n";
		cout<<"What is this employee's ID number?\t";
		cin>>empdatab[empnum].id;
		cout<<"\nWhat is this emplyee's First name?\t";
		cin>>empdatab[empnum].first;
		cout<<"\nWhat is this employee's Last name?\t";
		cin>>empdatab[empnum].last;
		cout<<"\nWhat is this employee's pay rate?\t";
		cin>>empdatab[empnum].payr;
		cout<<"\nWhat is this employee's hours?\t\t";
		cin>>empdatab[empnum].hours;
		cout<<"\nWhat is employee's gross pay?\t\t";
		cin>>empdatab[empnum].gross;
		cout<<"\nWhat is this employee's federal tax?\t";
		cin>>empdatab[empnum].ftax;
		cout<<"\nWhat is this employee's state tax?\t";
		cin>>empdatab[empnum].stax;
		cout<<"\nWhat is this employee's FICA?\t\t";
		cin>>empdatab[empnum].fica;
		cout<<"\nWhat is this employee's net pay?\t";
		cin>>empdatab[empnum].netp;
		
		int lft=19-empnum;
		cout<<"Thank you. Would you like to enter more information?\nYou have "<<lft<<" slots available. Press 'S' to stop, or press any key to continue entering information.\n";
		cin>>choice;
		if(choice='S')
		{
			//emptot=empnum;
			system("CLS");
			goto final;
		}
		else
			system("CLS");

		empnum++;
	}

final:
	cout<<"The full employee database will now be printed.\n";
	empnum=0;
	system("PAUSE");
	system("CLS");
	cout<<"ID#\tFirst\tLast\tRate\tHours\tGross\tFed\tState\tFICA\tNet\n";
	while(empnum<20)
	{
		cout<<empdatab[empnum].id<<"\t";
		cout<<empdatab[empnum].first<<"\t";
		cout<<empdatab[empnum].last<<"\t";
		cout<<empdatab[empnum].payr<<"\t";
		cout<<empdatab[empnum].hours<<"\t";
		cout<<empdatab[empnum].gross<<"\t";
		cout<<empdatab[empnum].ftax<<"\t";
		cout<<empdatab[empnum].stax<<"\t";
		cout<<empdatab[empnum].fica<<"\t";
		cout<<empdatab[empnum].netp<<"\t";
		empnum++;
	}

return 0;
}
Last edited on
You can use the STL sort algorithm:
http://cplusplus.com/reference/algorithm/sort/

All you'll need to add is an overloaded operator() function that returns true if the last name of one structure is "less" than the last name of the other structure.
Topic archived. No new replies allowed.