1. ## Sorting problem

Hi everyone!

If anybody could tell me why this sort is not working I would greatly appreciate it.

I'm trying to sort by last name, first name and middle initial. I have the last name sorted, but it seems to stop there. I've looked at this and tried so many different things for so long I think I may just be missing something silly.

Code:
```void Sort(vector<Names>& Access, vector<Entry>& Process, int& i)
// Pre condition: Add function has been accessed
// Post condition: Returns sorted Phone book
{
int  j;
struct Name temp[20];
struct Phone_Book_Entry temp2[20];
cout << Process.size();

for(i=0; i<=Process.size(); i++)
{
for(j=i+1; j<=Process.size(); j++)
{

if (Access[i].lastName > Access[j].lastName)
{

temp[i] = Access[i];
Access[i] = Access[j];
Access[j] = temp[i];

temp2[i] = Process[i];
Process[i] = Process[j];
Process[j] = temp2[i];

if (Access[i].firstName > Access[j].firstName)
{
temp[i] = Access[i];
Access[i] = Access[j];
Access[j] = temp[i];

temp2[i] = Process[i];
Process[i] = Process[j];
Process[j] = temp2[i];

if (Access[i].middleIni > Access[j].middleIni)
{
temp[i] = Access[i];
Access[i] = Access[j];
Access[j] = temp[i];

temp2[i] = Process[i];
Process[i] = Process[j];
Process[j] = temp2[i];

} // End 3rd if

} // End 2nd if

} // End 1st if

} // End for j

} // End for i

} // End Sort function```

2. I'm a little unsure of what you are trying to do. Are you sorting by last name and if they are equal, THEN sort by first name, and if that is equal, THEN sort by middle name? Right now its set up to check if one last name is greater than another, and if so, then swap them AND check to see if the first name is greater, if so then etc. I don't think this is what you want! If what you want is what I stated then you need to change it to something like:
Code:
```if (access[i].lastname>assess[j].lastname)
{
swap 'em
}
else if (access[i].lastname==access[j].lastname)
{
if (access[i].firstname>access[j].firstname)
{
swap'em
}
else if (access[i].firstname==access[j].firstname)
{
if (acess[i].middle==access[j].middle)
{
swap'em
}
}
}```

3. Thanks PJ!!! That did the trick!!!

4. No prob! Another way if you want to really shorten your code would be to write it in one if statement like so:
Code:
```if (firstname1>firstname2 || (firstname1==firstname2 && lastname1>lastname2) ||
(firstname1==firstname2 && lastname1==lastname2 && middle1>middle2))
{
swap'em
}```

5. ## Great plan!

Thanks for that advice, wow, what a difference!!! I have trouble with condensing my code, so this is absolutely welcomed information. I just need to learn how to think in this manner. I'm usually a quick learner, but for some reason I blank out on the if statements.