I think the other answers are good solutions to the problem but they don't quite answer the problem for your exact code and try to completely rework it. Hopefully this is able to better address your exact question with the fewest changes possible.
I am a bit unclear on weather or not you want one that sorts by last name then if needed first name or two separate sorts (one for first name and one for last) so I sorted it twice with each interpretation respectively.
For the first interpretation I added a second if statement that checked if the last names were the same and then sorted the first names if the last ones were already in order.
For the second interpretation I just changed it to sort by first names instead of last as your code already swapped both names at the same time.
#include <iostream>
#include <string>
using namespace std;
int main(){
const int NUM_PEOPLE = 7;
//sorting lastnames by length then first
cout << endl << "Version that sorts last then first names:" << endl;
string first_names[NUM_PEOPLE] = {"North","Franklin","Kanye","Ye","Bob","Tim","Arnold"};
string last_names[NUM_PEOPLE] = {"West","Smith","Johnson","West","Ash","Wolf","Kelly"};
for (int i = 0; i < NUM_PEOPLE; i++) {
cout << first_names[i] << "\t" << last_names[i] << endl;
}
string temp;
for (int pass = 0; pass < NUM_PEOPLE - 1; pass++){
for (int i = 0; i < NUM_PEOPLE - 1; i++){
if (last_names[i] > last_names[i + 1]) {
temp = last_names[i];
last_names[i] = last_names[i + 1];
last_names[i + 1] = temp;
temp = first_names[i];
first_names[i] = first_names[i + 1];
first_names[i + 1] = temp;
}
else if(last_names[i] == last_names[i + 1]
&& first_names[i] > first_names[i + 1]){
temp = first_names[i];
first_names[i] = first_names[i + 1];
first_names[i + 1] = temp;
}
}
}
cout << "After sort:" << endl;
for (int i = 0; i < NUM_PEOPLE; i++)
cout << last_names[i] << "\t" << first_names[i]<< endl;
//sorting by first name length only
cout << endl << "Version that only sorts by first name length:" << endl;
string first_names2[NUM_PEOPLE] = {"North","Franklin","Kanye","Ye","Bob","Tim","Arnold"};
string last_names2[NUM_PEOPLE] = {"West","Smith","Johnson","West","Ash","Wolf","Kelly"};
for (int i = 0; i < NUM_PEOPLE; i++) {
cout << first_names[i] << "\t" << last_names[i] << endl;
}
for (int pass = 0; pass < NUM_PEOPLE - 1; pass++){
for (int i = 0; i < NUM_PEOPLE - 1; i++){
if (first_names2[i] > first_names2[i + 1]) {
temp = last_names2[i];
last_names2[i] = last_names2[i + 1];
last_names2[i + 1] = temp;
temp = first_names2[i];
first_names2[i] = first_names2[i + 1];
first_names2[i + 1] = temp;
}
}
}
cout << "After sort:" << endl;
for (int i = 0; i < NUM_PEOPLE; i++)
cout << last_names2[i] << "\t" << first_names2[i]<< endl;
return 0;
}
Output:
Version that sorts last then first names:
North West
Franklin Smith
Kanye Johnson
Ye West
Bob Ash
Tim Wolf
Arnold Kelly
After sort:
Ash Bob
Johnson Kanye
Kelly Arnold
Smith Franklin
West North
West Ye
Wolf Tim
Version that only sorts by first name length:
Bob Ash
Kanye Johnson
Arnold Kelly
Franklin Smith
North West
Ye West
Tim Wolf
After sort:
Kelly Arnold
Ash Bob
Smith Franklin
Johnson Kanye
West North
Wolf Tim
West Ye
If this is what you are looking for I would still recommend looking at the other answers as some of them make improvements on the efficiency of your sort and do things in a more standard c++ way.