Sort vector of pointers to objects

Im creating a program for a race. The Race class has a vector of results and each element of that vector is a pointer to a result. The Result class has a Time and a pointer to a Participant. So in each race there are various results and it is a result for each participant.
The Time is a class that has hours, minutes and seconds.
How can I sort the vector of results from the result of the participant with the fastest time to the result of the participant with the slowest time?
My code is like this:

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
//.h file:

  class Time
{
    unsigned int hours;
    unsigned int minutes;
    unsigned int seconds;
public:
    Time(unsigned int h, unsigned int m, unsigned int s, unsigned int ms);
    Time();
    unsigned int gethours();
    unsigned int getMinuts();
    unsigned int getSeconds();
    string show();
};

class Participant {
    string name;
    unsigned int age;
    string country;
public:
    Participant(string n, unsigned int a, string c);
    string getName();
    string getCountry();
    int getAge();
    string show() const;
};

class Result {
    Participant *part;
    Time time;
public:
    Result(Participant  *p, Time t);
    Participant *getParticipant() const;
    Time getTime();
    string show();
    bool isFastest(Result& *res1, Result& *res2) const;
};

class Race {
    string name;
    float distance;
    vector<Result *> results;
public:
    Race(string nm, float dist);
    string getName();
    float getDistance();
    void sortResults();
    void addResult(Result *r);
    string showRaceResults();
    string show();
};

//.cpp file:

//not sure if I should pass the pointers as reference and not sure if this function must be const

bool Result::isFastest(Result& *res1, Result& *res2) const{ 
    if (res1.getTime()->gethours() < res2.getTime()->gethours())
        return true;
    else {
        if (res1.getTime()->gethours() > res2.getTime()->gethours())
            return false;
        else {
            if (res1.getTime()->getMinutes() < res2.getTime()->getMinutes())
                return true;
            else {
                if (res1.getTime()->getMinutes()) > res2.getTime()->getMinutes())
                    return false;
                else {
                    if (res2.getTime()->getSeconds() < res2.getTime()->getSeconds())
                        return true;
                    else {
                            return false;
                        }
                }
            }
        }
    }
}

void Race::sortResults() {
    sort (results.begin(), results.end(), isFastest);
}


What am I missing to get my code to work?
Use std::sort(). See the second variant here: http://www.cplusplus.com/reference/algorithm/sort/

In particular, note that you will need to write your own comparison function that drills down to the participant's and compares their times.
If you want to pass a pointer by reference (although you don't need to here) you've got the syntax wrong:

bool isFaster(Result*& a, Result*& b)

But, as I said, that isn't necessary. Nor is it necessary for your comparison function to be a member of Result. Your comparison is a typical less-than comparison so the name should reflect that, isFastest does not.

After numerous changes to make your code const correct, the function might look like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool isFaster(const Result* a, const Result* b)
{
    const Time aTime = a->getTime();
    const Time bTime = b->getTime();

    if (aTime.getHours() < bTime.getHours())
        return true;

    if (aTime.getMinutes() < bTime.getMinutes())
        return true;

    return aTime.getSeconds() < bTime.getSeconds();
}

void Race::sortResults() {
    sort(results.begin(), results.end(), isFaster);
}
Where exactly should I put the isFaster function? In the cpp, in the .h or in the main?
Im lost!
The implementation would go in the cpp file. There isn't necessarily any reason to include a declaration/prototype in the .h file since it doesn't need to be visible to code using the class interface.
I think cire's isFaster() may not be quite right. For example, if aTime is 10:11:12 and bTime is 12:20:21, it will think that aTime < bTime. The right code is more like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool isFaster(const Result* a, const Result* b)
{
    const Time aTime = a->getTime();
    const Time bTime = b->getTime();

    if (aTime.getHours() < bTime.getHours())
        return true;
    if (aTime.getHours() > bTime.getHours())
        return false;

    // hours are equal.
    if (aTime.getMinutes() < bTime.getMinutes())
        return true;
    if (aTime.getMinutes() > bTime.getMinutes())
        return false;

    // Minutes are equal
    return aTime.getSeconds() < bTime.getSeconds();
}
I think cire's isFaster() may not be quite right.

I think you're right. Thanks for the correction.
Thank you very much for your help.
I put the isFaster function in the .cpp file but I get two errors:

First in both this lines:
const Time aTime = a->getTime();
const Time bTime = b->getTime();
I get this error:
Multiple markers at this line
- Invalid arguments ' Candidates are: Time getTime() '
- passing 'const Result' as 'this' argument of 'Time Result::getTime()' discards qualifiers [- fpermissive]


Then in every line that has an if i get:
Multiple markers at this line
- Invalid arguments ' Candidates are: unsigned int getHoras() '
- Invalid arguments ' Candidates are: unsigned int getHoras() '
- passing 'const Tempo' as 'this' argument of 'unsigned int Tempo::getHoras()' discards
qualifiers [-fpermissive]
The problem is that *a and *b are const, but you're calling the non-const functions a->getTime(). Declare & define Result::getTime() to be const.

You will also need to declare/define the gethours, getMinutes and getSeconds methods of Time as const:
1
2
3
    unsigned int gethours() const;
    unsigned int getMinuts() const;
    unsigned int getSeconds() const;
Yes that solved all the problems!
Thank you very much!
Topic archived. No new replies allowed.