.sort() and #include<list>

Hello, i'm posting here because:
1. I'm just a beginner and I'm not sure how some C++ codes work;
2. I'm trying to learn C++ from a Dummy book, I have no teacher/classmates to ask about these;
I was using #include<list> and "class" here. I wonder if .sort() only works for numbers/int because i've tried using it on strings but it didn't work. Or maybe I put it in the wrong place.

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
class Account
{
public:
    Account(string sN, string sMN, string sLN, string sM, int nD, int nY)

...

    Account* pA;

    pA = new Account(getName(), getMidName(), getLastName(), getMonth(), getDate(), getYear());

    if (pA)
    {
        accList.push_back(pA);
        process(pA);
    }

...

void displayResults(list<AccountPtr>& accntList)
{
    accntList.sort();
    for (list<AccountPtr>::iterator iter = accntList.begin();
         iter != accntList.end();
         iter++)
    {
        AccountPtr pAccount = *iter;
        pAccount->display();
        total += pAccount->dBalance();
    }
}


I used .sort() before display but it didn't work. I also tried placing it at the getFunction() above but still it won't work. Should I place it inside the class or should I add some codes here?
i've tried using it on strings but it didn't work

No. It works.

1
2
3
4
5
6
7
list<string> myList;
myList.push_back("jafar");
myList.push_back("zbigz");
myList.push_back("bill");

myList.sort();
copy(myList.begin(),myList.end(),ostream_iterator<string>(cout," "));


bill jafar zbigz


The difference with your code is that you are trying to sort your own data type. Account
The standard function does not know how to compare 2 Accounts. So you have to tell it by writing a predicate function.

Account(string sN, string sMN, string sLN, string sM, int nD, int nY)

For example, i would consider sorting in ascending order with respect to sN.
1
2
3
4
bool sortingOder(Account &a, Account &b)
{
    return a.getSN() > b.getSN();
}


Then sort: accntList.sort(sortingOder);
Topic archived. No new replies allowed.