Selection sort by last name

Hello, I am working on an assignment that requires me to sort an a vector of names in alphabetical order using the last name. I don't know how to do that when the names are stored as "first last".
My code for sorting by first name is included for reference.

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
void SortByFirstName(vector<string> &students, vector<double> &grades){
   cout << "Sorting by first name" << endl;
   
   int i;
   int j;
   string temp;
   double tempGrades;
   int indexSmallest;
   
 for (i = 0; i < int(students.size() )- 1; ++i){
    indexSmallest = i;
    
    for (int j = i + 1; j < int(students.size()); ++ j){
       
       if (students[j] < students[indexSmallest]){
          indexSmallest = j;
       }
    }
    if (indexSmallest != i){
    temp = students[i];
    tempGrades = grades[i];
    students[i] = students[indexSmallest];
    grades[i] = grades[indexSmallest];
    students[indexSmallest] = temp;
    grades[indexSmallest] = tempGrades;
      }
   }
}
you need to split the names, either store them as 2 strings, or split off the last name in the sort comparison (woefully inefficient for more than a small # of names).
Thanks, but I don't think I am allowed to store them as two different strings.
To get the last name from two names separated by a space:

 
    string lastName = students[i].substr(students[i].find(' ') + 1);

Thanks, that worked perfect.
Topic archived. No new replies allowed.