I wrote this for a friend of mine for his C++ class. It's not exactly wonderful. I'm putting it here for posterity.
He had written a student database program that just stored First Name, Last Name, and ID in strings and created a class
array within the CourseSection class to hold all the information. He need to write a sort function to sort them all by
last name, but he sucks and couldn't hack it. I wrote this long-ass one for him, which was my favorite implementation.
It just felt more crisp than anything, especially for being patched into someone else's code.
Anyway, I just figured I'd put it somewhere.
/****************************************************************
* CourseSection::sort() *
* *
* Operations: *
* 1. Create Struct and copy students into it. It has last and *
* next elements to keep track of order, element to element.*
* *
* 2. Cycles through students, compares them with the previous *
* and next one, and sets lastabove to 1 if the current one *
* is higher or 0 if the last or next one is higher *
* *
* 3. Moves the current student up or down in the order, if *
* needed. Jumps back an iteration if it moved up. Stays on *
* the same iteration if it moved down. (Down is further *
* away from A, further down in the order.) *
* *
* 4. Finally, uses new sub in Student readjust the order in *
* master student object (m_students) *
****************************************************************/
void CourseSection::sort() {
// Struct to hold all the information from Student m_students
struct ostudents {
string currfirst;
string currlast;
string currid;
string lastfirst;
string lastlast;
string lastid;
bool lastabove; // 1 if the previous entry is higher (in asciibetical) than the current one
string nextfirst;
string nextlast;
string nextid;
bool nextabove; // 1 if the next entry is lower than the current one
} l_students[m_studentCount];
for (int i = 0; i < m_studentCount; i++) { // Assign all the elements in m_students to the
l_students[i].currfirst = m_students[i].getFirstName(); // corresponding l_students element
l_students[i].currlast = m_students[i].getLastName();
l_students[i].currid = m_students[i].getId();
if (i != 0) {
l_students[i].lastfirst = m_students[i - 1].getFirstName();
l_students[i].lastlast = m_students[i - 1].getLastName();
l_students[i].lastid = m_students[i - 1].getId();
} else {
l_students[i].lastfirst = "AAAAAAAAAAAAAAAAAAA"; // This sucks, but, God willing, there won't
l_students[i].lastlast = "AAAAAAAAAAAAAAAAAAA"; // be a student who's really startled when he
l_students[i].lastid = "$$$$$$$$$$$$$$$$$$$"; // fills out his school application.
}
if (i < (m_studentCount - 1)) {
l_students[i].nextfirst = m_students[i + 1].getFirstName();
l_students[i].nextlast = m_students[i + 1].getLastName();
l_students[i].nextid = m_students[i + 1].getId();
} else {
l_students[i].nextfirst = "zzzzzzzzzzzzzzzzzzz"; // Now hopefully there is a better way to do this.
l_students[i].nextlast = "zzzzzzzzzzzzzzzzzzz"; // This is just to be last in ASCIIbetical order
l_students[i].nextid = "zzzzzzzzzzzzzzzzzzz"; // so that the last will be the last;
}
l_students[i].lastabove = 0;
l_students[i].nextabove = 0;
}
/* Uses compare member function from the String class to compare
strings with ASCIIbetical precedence */
for (int i = 1; i < m_studentCount; i++) {
if (l_students[i].currlast.compare(l_students[i].lastlast) > 0) { // Check if current last name is higher
l_students[i].lastabove = 1; // or lower than the last last name
} else if (l_students[i].currlast.compare(l_students[i].lastlast) < 0) {
l_students[i].lastabove = 0;
} else {
if (l_students[i].currfirst.compare(l_students[i].lastfirst) > 0) { // If the last names are the same, check the firsts.
l_students[i].lastabove = 1;
} else if (l_students[i].currfirst.compare(l_students[i].lastfirst) < 0) {
l_students[i].lastabove = 0;
} else {
if (l_students[i].currid.compare(l_students[i].lastid) > 0) { // Compare the IDs if necessary
l_students[i].lastabove = 1;
} else if (l_students[i].currid.compare(l_students[i].lastid) < 0) {
l_students[i].lastabove = 0;
} else {
// WHAT THE FUCK?
}
}
}
if (l_students[i].currlast.compare(l_students[i].nextlast) > 0) { // Check if current last name is higher
l_students[i].nextabove = 1; // or lower than the next last name
} else if (l_students[i].currlast.compare(l_students[i].nextlast) < 0) {
l_students[i].nextabove = 0;
} else {
if (l_students[i].currfirst.compare(l_students[i].nextfirst) > 0) { // If the last names are the same, check the firsts.
l_students[i].nextabove = 0;
} else if (l_students[i].currfirst.compare(l_students[i].nextfirst) < 0) {
l_students[i].nextabove = 1;
} else {
if (l_students[i].currid.compare(l_students[i].nextid) > 0) { // Compare the IDs if necessary
l_students[i].nextabove = 1;
} else if (l_students[i].currid.compare(l_students[i].nextid) < 0) {
l_students[i].nextabove = 0;
} else {
// Hopefully Impossible
}
}
}
string firstfirst;
string firstlast;
string firstid;
string firstlastfirst;
string firstlastlast;
string firstlastid;
string firstnextfirst;
string firstnextlast;
string firstnextid;
string secondfirst;
string secondlast;
string secondid;
string secondlastfirst;
string secondlastlast;
string secondlastid;
string secondnextfirst;
string secondnextlast;
string secondnextid;
if (l_students[i].lastabove == 0) { // If the one is higher than another, swap their positions.
//////////////////////////////////////////
// Assign the temp variables all around //
//////////////////////////////////////////
firstfirst = l_students[i].currfirst;
firstlast = l_students[i].currlast;
firstid = l_students[i].currid;
firstlastfirst = l_students[i].lastfirst;
firstlastlast = l_students[i].lastlast;
firstlastid = l_students[i].lastid;
firstnextfirst = l_students[i].nextfirst;
firstnextlast = l_students[i].nextlast;
firstnextid = l_students[i].nextid;
secondfirst= l_students[i - 1].currfirst;
secondlast = l_students[i - 1].currlast;
secondid = l_students[i - 1].currid;
secondlastfirst = l_students[i - 1].lastfirst;
secondlastlast = l_students[i - 1].lastlast;
secondlastid = l_students[i - 1].lastid;
secondnextfirst = l_students[i - 1].nextfirst;
secondnextlast = l_students[i - 1].nextlast;
secondnextid = l_students[i - 1].nextid;
///////////////////////
// Swap elements out //
///////////////////////
l_students[i].currfirst = secondfirst;
l_students[i].currlast = secondlast;
l_students[i].currid = secondid;
l_students[i].lastfirst = secondnextfirst;
l_students[i].lastlast = secondnextlast;
l_students[i].lastid = secondnextid;
l_students[i].nextfirst = firstnextfirst;
l_students[i].nextlast = firstnextlast;
l_students[i].nextid = firstnextid;
l_students[i - 1].currfirst = firstfirst;
l_students[i - 1].currlast = firstlast;
l_students[i - 1].currid = firstid;
l_students[i - 1].lastfirst = secondlastfirst;
l_students[i - 1].lastlast = secondlastlast;
l_students[i - 1].lastid = secondlastid;
l_students[i - 1].nextfirst = firstlastfirst;
l_students[i - 1].nextlast = firstlastlast;
l_students[i - 1].nextid = firstlastid;
if ((i - 2) >= 0) {
l_students[i - 2].nextfirst = firstfirst;
l_students[i - 2].nextlast = firstlast;
l_students[i - 2].nextid = firstid;
}
if ((i + 1) < m_studentCount) {
l_students[i + 1].lastfirst = secondfirst;
l_students[i + 1].lastlast = secondlast;
l_students[i + 1].lastid = secondid;
}
// Redo the same and previous iteration, so the new positions
// are redone until they're in the right positions
i--; i--;
if (i < 0) i = 0; // The next iteration should be 1 or higher
} else if (l_students[i].nextabove == 1) {
//////////////////////////////////////////
// Assign the temp variables all around //
//////////////////////////////////////////
firstfirst = l_students[i].currfirst;
firstlast = l_students[i].currlast;
firstid = l_students[i].currid;
firstlastfirst = l_students[i].lastfirst;
firstlastlast = l_students[i].lastlast;
firstlastid = l_students[i].lastid;
firstnextfirst = l_students[i].nextfirst;
firstnextlast = l_students[i].nextlast;
firstnextid = l_students[i].nextid;
secondfirst= l_students[i + 1].currfirst;
secondlast = l_students[i + 1].currlast;
secondid = l_students[i + 1].currid;
secondlastfirst = l_students[i + 1].lastfirst;
secondlastlast = l_students[i + 1].lastlast;
secondlastid = l_students[i + 1].lastid;
secondnextfirst = l_students[i + 1].nextfirst;
secondnextlast = l_students[i + 1].nextlast;
secondnextid = l_students[i + 1].nextid;
///////////////////////
// Swap elements out //
///////////////////////
l_students[i].currfirst = secondfirst;
l_students[i].currlast = secondlast;
l_students[i].currid = secondid;
l_students[i].lastfirst = firstlastfirst;
l_students[i].lastlast = firstlastlast;
l_students[i].lastid = firstlastid;
l_students[i].nextfirst = secondlastfirst;
l_students[i].nextlast = secondlastlast;
l_students[i].nextid = secondlastid;
l_students[i + 1].currfirst = firstfirst;
l_students[i + 1].currlast = firstlast;
l_students[i + 1].currid = firstid;
l_students[i + 1].lastfirst = firstnextfirst;
l_students[i + 1].lastlast = firstnextlast;
l_students[i + 1].lastid = firstnextid;
l_students[i + 1].nextfirst = secondnextfirst;
l_students[i + 1].nextlast = secondnextlast;
l_students[i + 1].nextid = secondnextid;
if ((i - 1) >= 0) { // The previous entry to match the swapped
l_students[i - 1].nextfirst = secondfirst;
l_students[i - 1].nextlast = secondlast;
l_students[i - 1].nextid = secondid;
}
if ((i + 2) < m_studentCount) { // next entry
l_students[i + 2].lastfirst = firstfirst;
l_students[i + 2].lastlast = firstlast;
l_students[i + 2].lastid = firstid;
}
i--;
}
}
for (int i = 0; i < m_studentCount; i++) { // Apply it to student object
m_students[i].setStudentInfo(
l_students[i].currfirst,
l_students[i].currlast,
l_students[i].currid
);
}
// SORTED!!
}
void Student::setStudentInfo(string firstname, string lastname, string id) {
m_firstName = firstname;
m_lastName = lastname;
m_id = id;
}
void Student::setFirstName(string firstname) {
m_firstName = firstname;
}
void Student::setLastName(string lastname) {
m_lastName = lastname;
}
void Student::setId(string id) {
m_id = id;
}