Selection sort by last name?

The current code below shows me sorting the string members of an array of structures in ascending order. I am sorting by the entire string, the entire name of the directors. What I want to do is do the same sort, but with the last names of the directors. So would I HAVE to use c-strings, and how? Or maybe there's a member function to let me cut out everything but the director's last name? How?

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

struct Movie
{
    int timesCalled;
    int year;
    string name;
    string director;
};

Movie *readMovieInfo(int &size);
void printDirectors(Movie *array, int size);
void selectionSortAscending(Movie *array, int size);
int binarySearch(Movie *array, int size, string value);
void printResults(int result, int size, Movie *array);
void writeMovies(Movie *array, int size);


int main()
{
    int size = 0, result = 0;
    string value;
    Movie *movieList;

    movieList = readMovieInfo(size);
    printDirectors(movieList, size);
    selectionSortAscending(movieList, size);
    cout << endl << endl;
    for (int i = 1; i <= size; i++)
    {
        cout << left << setw(20) << movieList[i - 1].director << "\t";
        if (i % 3 == 0)
            cout << endl;
    }

/*
    while (value != "QUIT" || value != "quit")      // Loop until user wants to quit
    {
        cout << endl << endl << "Enter a director's name (or enter QUIT to quit): ";
        getline(cin, value);
        if (value == "QUIT" || value == "quit")
            break;

        result = binarySearch(movieList, size, value);
        printResults(result, size, movieList);
    }
    writeMovies(movieList, size);
    delete [] movieList;
*/
    return 0;
}

//********************************************************************************
// Definition of function *readMovieInfo.
// This function uses an input file (chosen by user) to read in data for a
// dynamically allocated array of structures's members.
//********************************************************************************

Movie *readMovieInfo(int &size)
{
    ifstream inFile;
    string file;
    Movie *movieList;

    cout << "What is the name of the file you would like to open? ";
    getline(cin, file);
    file.append(".txt");
    inFile.open(file.c_str());
    if(!inFile)                                        // File validation
    {
        cout << "Can't open the input file!" << endl;
        exit(111);
    }

    inFile >> size;
    movieList = new Movie[size];

    for (int i = 0; i < size; i++)
    {
        inFile >> movieList[i].year;

        getline(inFile, movieList[i].name, '\"');
		getline(inFile, movieList[i].name, '\"');

		getline(inFile, movieList[i].director, '\"');
		getline(inFile, movieList[i].director, '\"');

		movieList[i].timesCalled = 0;        
    }
    inFile.close();

    return movieList;
}

//***********************************************************************
// Definition of function printDirectors.                               *
// This function simply prints the available directors that are found   *
// in the array of structures *array in a three director per row format.*
//***********************************************************************

void printDirectors(Movie *array, int size)
{
    cout << "Here are the available directors:" << endl << endl;

    for (int i = 1; i <= size; i++)
    {
        cout << left << setw(20) << array[i - 1].director << "\t";
        if (i % 3 == 0)
            cout << endl;
    }
}

//***********************************************************************
// Definition of function selectionSortAscending                        *
// This function performs a selection sort on the array of structures
// *array by director name.
//***********************************************************************

void selectionSortAscending(Movie *array, int size)
{
   string minValue;
   int  startScan, minIndex;

   for (startScan = 0; startScan < (size - 1); startScan++)
   {
      minIndex = startScan;
      minValue = array[startScan].director;
      for(int index = startScan + 1; index < size; index++)
      {
         if (array[index].director < minValue)
         {
            minValue = array[index].director;
            minIndex = index;
         }
      }
      array[minIndex].director = array[startScan].director;
      array[startScan].director = minValue;
   }
} 


The text file with sorted directors:
1
2
3
4
5
6
7
8
9
10
11
10
2008	"The Dark Knight"			           "Christopher Nolan"
1999    "Fight Club"                                       "David Fincher"
1972    "The Godfather"                                    "Francis F. Coppola"
1974	"The Godfather: Part II"		           "Francis F. Coppola"
1994    "The Shawshank Redemption"                         "Frank Darabont"
2003	"The Lord of the Rings: The Return of the King"	   "Peter Jackson"
1994	"Pulp Fiction"		                           "Quentin Tarantino"
1966	"The Good, the Bad and the Ugly"	           "Sergio Leone"
1957	"12 Angry Men"		                           "Sidney Lumet"
1993	"Schindler's List"			           "Steven Spielberg"



The file with unsorted directors:
1
2
3
4
5
6
7
8
9
10
11
10
1999    "Fight Club"                                       "David Fincher"
1972    "The Godfather"                                    "Francis F. Coppola"
1994    "The Shawshank Redemption"                         "Frank Darabont"
2003	"The Lord of the Rings: The Return of the King"	   "Peter Jackson"
1957	"12 Angry Men"		                           "Sidney Lumet"
2008	"The Dark Knight"			           "Christopher Nolan"
1994	"Pulp Fiction"		                           "Quentin Tarantino"
1966	"The Good, the Bad and the Ugly"	           "Sergio Leone"
1974	"The Godfather: Part II"		           "Francis F. Coppola"
1993	"Schindler's List"			           "Steven Spielberg"


So for example, Instead of sorting with "Francis F. Coppola" I'd like to sort with "Coppola"
Last edited on
Names are nasty. Is the surname of "Robert Downey Jr." really "Jr."? http://nwalsh.com/tex/texhelp/bibtx-23.html

But lets start with "last word is the surname". Look at http://www.cplusplus.com/reference/string/string/find_last_of/
Last whitespace in a string should be just before the last word starts (unless you have trailing whitespace).
If there is no whitespace, then there is only one word.

Given position, you could substr, but http://www.cplusplus.com/reference/string/string/compare/
can do two in in one.

Btw, Sofia and Francis F. don't sort by surname alone.
Last edited on
So you're saying that it would work with the compare function? What exactly does the string compare with with the other string? And after I find the last whitespace with find_last_of, can I delete the preceding chars and the whitespace, leaving me with the last name?
Your selection sort algorithm only swaps the director's name not the movie associated with the director. To swap the movies as well ( or just any swapping you are doing ), use std::swap
http://en.cppreference.com/w/cpp/algorithm/swap
Topic archived. No new replies allowed.