Sorting an Array

I'm trying to sort an array by students' last names and GPA. The sortLastName function isn't working at all, while the sortGPA function is kind of working. It's sorting by GPA, but my output is duplicating student information as follows:
Student List
Last Name: Gonzales || First Name: Mariana || Student ID: 168790 || Student E-mail: m.gonzales18@spartans.nsu.edu || Student GPA: 4
Last Name: Gonzales || First Name: Mariana || Student ID: 168790 || Student E-mail: m.gonzales18@spartans.nsu.edu || Student GPA: 4
Last Name: Gonzales || First Name: Mariana || Student ID: 168790 || Student E-mail: m.gonzales18@spartans.nsu.edu || Student GPA: 4
Last Name: Williams || First Name: Anita || Student ID: 2985465 || Student E-mail: a.williams@spartans.nsu.edu || Student GPA: 3.64
Last Name: Williams || First Name: Anita || Student ID: 2985465 || Student E-mail: a.williams@spartans.nsu.edu || Student GPA: 3.64
Last Name: Ronsinson || First Name: Taylor || Student ID: 3278976 || Student E-mail: t.robinson@spartans.nsu.edu || Student GPA: 3.55
Last Name: Clark || First Name: Allen || Student ID: 1094567 || Student E-mail: a.clark@spartans.nsu.edu || Student GPA: 3.48
Last Name: Turner || First Name: Tavon || Student ID: 318796 || Student E-mail: t.turner@spartans.nsu.edu || Student GPA: 3.2
Last Name: Jenkins || First Name: Nelson || Student ID: 289563 || Student E-mail: n.jenkins@spartans.nsu.edu || Student GPA: 3

Can anyone tell me what i'm doing wrong? Thanks in advance.

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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

struct studentInfo
{
	string firstName;
	string lastName;
	unsigned int studentID;
	string email;
	float gpa;
};

class studentsProc
{
	public:
		void studentList();
		void loadList();
		void retrieveStudent();
		void insertStudent();
		void deleteStudent();
		void printStudent();
		void displayStudent();
		void sortLastName();
		void sortGpa();
		studentsProc();
	
	private:
		studentInfo students[40];
		int size;
		void printList() const;
};

studentsProc::studentsProc()
{
	size = 0;
}

void studentsProc::studentList()
{
	cout << "0. Smith, Jefferey" << endl;
	cout << "1. Lee, Jackson" << endl;
	cout << "2. Gonzales, Mariana" << endl;
	cout << "3. Jones, Mike" << endl;
	cout << "4. Wiliams, Anita" << endl;
	cout << "5. Ronsinson, Taylor" << endl;
	cout << "6. Clark, Allen" << endl;
	cout << "7. Turner, Tavon" << endl;
	cout << "8. Jenkins, Nelson" << endl << endl;
}

void studentsProc::loadList()
{
	ifstream infile;
	
	infile.open("students.dat");
	size = 0;
	
	while (!infile.eof())
	{
			infile >> students[size].lastName;
			infile >> students[size].firstName;
			infile >> students[size].studentID;
			infile >> students[size].email;
			infile >> students[size].gpa;
			size++;

	}
infile.close();
}

void studentsProc::retrieveStudent()
{
	int number;
	studentsProc::studentList();
	cout << "Please Enter the Position of the Student you Wish to Retrieve " << endl;
	cin >> number;
	{
		cout << "Student Name: " << students[number].firstName << " " << students[number].lastName << endl;
	}
}

void studentsProc::insertStudent()
{
int position;

cout << "Enter the Position of the New Student you Would Like to Add: " << endl;
cin >> position; 
cout << endl;
cout << "Enter the Following Information About The Student: " << endl;
cout << "First Name: "; cin >> students[position].firstName;
cout << "Last Name: "; cin >> students[position].lastName;
cout << "Student ID: "; cin >> students[position].studentID;
cout << "E-Mail: "; cin >> students[position].email;
cout << "GPA: "; cin >> students[position].gpa;
cout << "The New List Is: " << endl;
studentsProc::displayStudent();
}

void studentsProc::deleteStudent()
{
int pos;

cout << "Enter The Position of the Student you Would Like to Delete:" << endl;
cin >> pos;
for (int i = pos; i < size - 1; i++)
{
	students[i] = students[i+1];
}

size--;

cout << "The New List Is: " << endl;
studentsProc::displayStudent();
}

void studentsProc::printStudent()
{
	int number;
	studentsProc::studentList();
	cout << "Please Enter the Number of the Student's Record You Wish to View " << endl;
	cin >> number;
	{
		cout << "First Name: " << students[number].firstName << " || "
		<< "Last Name: " << students[number].lastName << " || "
		<< "Student ID: " << students[number].studentID << " || "
		<< "Student E-mail: " << students[number].email << " || "
		<< "Student GPA: " << students[number].gpa << endl;
	}
}

void studentsProc::displayStudent()
{
	studentsProc::printList();
}

void studentsProc::printList() const
{
	cout << "Student List" << endl;
	for (int i = 0; i < size; i++)
	{
		cout << "Last Name: " << students[i].lastName << " || "
			 << "First Name: " << students[i].firstName << " || "
			 << "Student ID: " << students[i].studentID << " || "
			 << "Student E-mail: " << students[i].email << " || "
			 << "Student GPA: " << students[i].gpa << endl;
	}	
}

void studentsProc::sortLastName()
{
for(int i=0; i<size; i++){

for(int j=i; j<size; j++){

if(students[i].lastName.compare(students[i].lastName)>0){

studentInfo temp = students[i];

students[i] = students[j];

students[j] = students[i];

}

}

}

studentsProc::printList();
}

void studentsProc::sortGpa()
{
for(int i=0; i < size; i++){

for(int j=i; j < size; j++){

if(students[i].gpa<students[j].gpa){

studentInfo temp = students[i];

students[i] = students[j];

students[j] = students[i];

}

}

}
cout << "The Student List Sorted By GPA is: " << endl;
studentsProc::printList();
}

int main()
{
	studentsProc student1;
	student1.loadList();
	int choice;

    do
    {
      cout << "Please Choose an Option from this Menu" << endl << endl;
      cout << "1. Retrieve and Print a Student from the Student List " << endl;
      cout << "2. Insert a Student into the Student List " << endl;
      cout << "3. Delete a Student from the Student List " << endl;
      cout << "4. Print the Contents of a Student Record " << endl;
      cout << "5. Print the Contents for a List of Students " << endl;
      cout << "6. View the Students List Sorted by Last Name " << endl;
      cout << "7. View the Students List Sorted by GPA " << endl;
      cin >> choice;
      cout << endl;
      
      switch (choice) 
      {
             case 1:     student1.retrieveStudent();
             			 break;
             case 2: 	 student1.insertStudent();
             			 break;
             case 3:	 student1.deleteStudent();
             			 break;
             case 4: 	 student1.printStudent();
             			 break;
             case 5: 	 student1.displayStudent();
             			 break;
             case 6:	 student1.sortLastName();
             		     break;
             case 7:     student1.sortGpa();
			    		 break;             		    
             default:    cout << "Please enter a value 1-7" << endl;
      }//end switch

   cout << endl << endl;  
   } while (choice != 7);
	
return 0;
}

Content in students.dat file:
Smith Jefferey 891789 j.smith@spartans.nsu.edu 3.75
Lee Jackson 3678902 j.lee@spartans.nsu.edu 3.66
Gonzales Mariana 168790 m.gonzales18@spartans.nsu.edu 4.0
Jones Mike 8973125 m.jones143@spartans.nsu.edu 3.1
Williams Anita 2985465 a.williams@spartans.nsu.edu 3.64
Ronsinson Taylor 3278976 t.robinson@spartans.nsu.edu 3.55
Clark Allen 1094567 a.clark@spartans.nsu.edu 3.48
Turner Tavon 318796 t.turner@spartans.nsu.edu 3.2
Jenkins Nelson 289563 n.jenkins@spartans.nsu.edu 3.0
1
2
3
students[i] = students[j];

students[j] = students[i];


Is it possible for students[i] and students[j] to be different after this?
You could also compare your solution to:
http://www.cplusplus.com/reference/utility/swap/

(Assuming that the std::swap performs logically equivalent operation to your problematic code. Does it?)
Topic archived. No new replies allowed.