Sorting

Hello, I am having troubles using sort inside a void function. I am required to use void sortByAuthor(Book books[], int count) and somehow apply it to showBooksByAuthor(Book books[], int count, string name).

I have tried looking for some help online but I can't find an example of how to use sort inside a void and then apply it elsewhere. Any hints or links to examples would be great. Thanks!


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
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
#include <algorithm>


using namespace std;

struct Book
{
    string title;
    string author;
};


const int ARRAY_SIZE = 1000;
Book books [ARRAY_SIZE];

int loadData (Book books[]);
void showAll (Book books[], int count);
int showBooksByAuthor (int string);
int showBooksByTitle (int string);
void sortByTitle (Book books[], int count);
void sortByAuthor (Book books[], int count);

int showBooksByAuthor(Book books[], int count, string name)
{
    int authorCount = 0;
    int match;

    for(int i = 0; i < 1000; i++)
    {
        match = books[i].author.find(name);
        if(match != string::npos)
        {
            cout << endl << books[i].title << " (" << books[i].author << ")" << endl;
            authorCount++;
        }
    }
    cout << endl << authorCount << " records found." << endl;
    return 0;
}

void sortByAuthor (Book books[], int count)
{
    int showBooksByAuthor(Book books);
    sort(Book books::begin(showBooksByAuthor), Book books::end(showBooksByAuthor));

}
Last edited on
Looks like you have never use std::sort.
Have a look at
http://www.dreamincode.net/forums/topic/319433-stl-algorithms-tutorial-1-using-sort/
under Sorting User Made Types - roughly in the middle of the page
I have not used std::sort before. I am looking at the Sorting User Made Types.

 
bool sortByName(const Person &lhs, const Person &rhs) { return lhs.name < rhs.name; }


I am having trouble figuring out which part of this code relates to my own.

1
2
3
4
5
6
7
8
9
10
11
12
struct Book // Book would equal Person
{
    string title;
    string author;
};

struct Person
{
    string name;
    int age;
    string favoriteColor;
};


My question is the &lhs / &rhs. I am not sure where this relates to my code. As I said my void function has to remain as is.

Perhaps I am just missing the { return } ?

Topic archived. No new replies allowed.