Linked List not Sorting

Hello,
I am trying to make a linked list program that will take two parameters and sort first by age (least to greatest), then by name (alphabetically). The lists sorts fine until I enter an age greater than some values but less than others. At that point it does not sort correctly anymore.
For example, I entered:
"Three - 20, Four - 20, John - 68, Doe - 50"
and it sorted and displayed:
"20 Four", 20 Three, 68 John, 50 Doe."

Through trial and error, I am fairly certain that the problem lies in the "greater than" operator function, but I am not sure what exactly is failing.

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
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

class node
{
    public:
        int age;
        string name;
        node *next;
        node(string input, int number) {name = input; age = number; next = NULL;}
        int operator>(const node &other);
        int operator<(const node &other);
};

void delete_list(node *p_node);
int get_num();

int main(int argc, char *argv[])
{
    node *root = NULL;
    node *p_node, *new_node;
    string a_name;
    int age = 1;

    while (true)
    {
        cout << "Enter a name (or ENTER to exit): ";
        getline(cin, a_name);
        if (a_name.empty())
        {
            break;
        }
        cout << "Enter age: ";
        age = get_num();

        new_node = new node(a_name, age);

        //If list is new or node goes at beginning then insert as root node. Otherwise search for a node to insert node AFTER
        if (root == NULL || *new_node < *root)
        {
            new_node->next = root;
            root = new_node;
        }
        else
        {
            p_node = root;
            while (p_node->next && *new_node > *p_node)
            {
                p_node = p_node->next;
            }
            new_node->next = p_node->next;
            p_node->next = new_node;
        }
    }
    p_node = root;
    while (p_node)
    {
        cout << p_node->age << ", "  << p_node->name << endl;
        p_node = p_node->next;
    }

    delete_list(root);
    return 0;
}

void delete_list(node *p_node)
{
    node *temp;
    while (p_node)
    {
        temp = p_node;
        p_node = p_node->next;
        delete temp;
    }
}

int node::operator>(const node &other)
{
    if (this->age == other.age)
    {
        if (this->name > other.name)
            return 1;
        else
            return 0;
    }
    else if(this->age > other.age)
        return 1;
    else
        return 0;
}

int node::operator<(const node &other)
{
    if (this->age == other.age)
    {
        if (this->name < other.name)
            return 1;
        else
            return 0;
    }
    else if (this->age < other.age)
        return 1;
    else
        return 0;
}

int get_num()
{
    string s;
    getline(cin, s);
    return atoi(s.c_str());
}


Any help at all would be greatly appreciated.
It seems to me that you're inserting your new node after the first greater than it, not before.
I think that if you changed the condition on line 50 to > *p_node->next, it should be fine.
That fixed it, thank you very much!
Topic archived. No new replies allowed.