I am trying to accomplish the following task:
List the students in alphabetic order, sorted by last name. Do not change the given case of the names. Do not change the output file format. (Firstname Lastname) Just print the records in order by last name, i.e.
Annie J
Martin K
Toby L
This sort needs to be true alphabetical (not just the "lexicographical" sort).
The data was read in from a file and passed through a virtual function depending on what course this student was enrolled in. Here's what I have.
for (int i = 1; i < numStudents; i++)
{
if (( list[i] -> getLastname() ) < ( list[i - 1] -> getLastname() ))
{
Student *temp = list[i - 1];
ist[i - 1] = list[i];
list[i] = temp;
}
}
I've been working on this for a while now and I'm worried I've gone about this all wrong. Any tips/pointers appreciated!
std::sort? It also appears that your program is likely to access outside the bounds of yourlistarray, which is pretty dangerous too.operator<will do a lexicographic comparison. So"amy"will compare greater than"Tom"because'a'(ASCII 97) is greater than'T'(ASCII 84).