How do I overload the < operator to make my sorting algorithm work?

error: no match for ‘operator<’ (operand types are ‘bool’ and ‘const string {aka const std::__cxx11::basic_string<char>}’)
const addressCard &obj10) {return obj1.name < obj2.name < obj3.street < obj4.street <
~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~


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

using namespace std;

struct addressCard
{
string name;
string street;
string town;
string county;
string postCode;
int telephoneNumber;
};

bool sortByName(const addressCard &obj1,const addressCard &obj2, const addressCard &obj3, 
const addressCard &obj4, const addressCard &obj5, const addressCard &obj6,
const addressCard &obj7, const addressCard &obj8, const addressCard &obj9,
const addressCard &obj10) {return obj1.name < obj2.name < obj3.street < obj4.street <
obj5.town < obj6.town < obj7.county < obj8.county < obj9.postCode < obj10.postCode;}

const unsigned numberOfPeople = 4;

int main()
{
vector<addressCard> people(numberOfPeople);

for (vector<addressCard>::size_type i = 0; i!=numberOfPeople; ++i)
{
cout <<"Person# " <<i+1<< " name: " << endl;
cin>> people[i].name;

cout <<"Person# " <<i+1<< " street: " << endl;
cin>> people[i].street;

cout <<"Person# " <<i+1<< " town: " << endl;
cin>> people[i].town;

cout <<"Person# " <<i+1<< " county: " << endl;
cin>> people[i].county;

cout <<"Person# " <<i+1<< " postCode: " << endl;
cin>> people[i].postCode;
}

cout <<"\n\n";

sort(people.begin(), people.end(), sortByName);
for (Person &n : People)
{
cout << n.name << endl;
cout << n.street << endl;
cout << n.town << endl;
cout << n.county << endl;
cout << n.postCode << endl;

cout <<"\n\n";
}

return 0;

}
Last edited on
You cannot "combine" logical operators like that in C++.
e.g. if (a < b < c) is wrong. You must do if (a < b && b < c).

That being said, in your case you should avoid that altogether.
When you pass in the function pointer to std::sort, it expects a function that takes in 2 objects, not.... 10...

Simply:
1
2
3
4
bool sortByName(const addressCard &obj1, const addressCard &obj2)
{
    return obj1.name < obj2.name;   
}



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

using namespace std;

struct addressCard
{
    string name;
    string street;
    string town;
    string county;
    string postCode;
    int telephoneNumber;
};

bool sortByName(const addressCard &obj1, const addressCard &obj2)
{
    return obj1.name < obj2.name;   
}


int main()
{
    const unsigned numberOfPeople = 4;
    vector<addressCard> people(numberOfPeople);
    
    for (vector<addressCard>::size_type i = 0; i != numberOfPeople; i++)
    {
        cout <<"Person# " <<i+1<< " name: " << endl;
        cin>> people[i].name;
        
        cout <<"Person# " <<i+1<< " street: " << endl;
        cin>> people[i].street;
        
        cout <<"Person# " <<i+1<< " town: " << endl;
        cin>> people[i].town;
        
        cout <<"Person# " <<i+1<< " county: " << endl;
        cin>> people[i].county;
        
        cout <<"Person# " <<i+1<< " postCode: " << endl;
        cin>> people[i].postCode;
    }
    
    cout <<"\n\n";

    sort(people.begin(), people.end(), sortByName);
    for (addressCard &n : people)
    {
        cout << n.name << endl;
        cout << n.street << endl;
        cout << n.town << endl;
        cout << n.county << endl;
        cout << n.postCode << endl;
        
        cout <<"\n\n";
    }
    
    return 0;
}
Last edited on
I might be wrong, but at first sight it seems you're looking for a code like this:
http://www.cplusplus.com/forum/beginner/209244/2/#msg984508

But if i get 2 names and addresses that are the same but have a different county how would i make it sort that?
1
2
3
4
5
6
7
8
9
10
11
bool sortByNameThenCountry(const addressCard &obj1, const addressCard &obj2)
{
    if (obj1.name == obj2.name)
    {
        return obj1.country < obj2.country
    }
    else
    {
        return obj1.name < obj2.name;   
    }
}


(Deleted my previous post because I misunderstood your follow-up question.)

If you want to sort by more properties, you need to continue to chain them in this manner.
Last edited on
Thanks foe your help. I'm way to much of a noob to have tried that on my own :-)
You really should define a specific sorting order for your data.

For example, given:
1
2
3
4
5
string name;
string street;
string town;
string county;
string postCode;

It seems to me that you already have a basic sort order.

Group by zip.
Inside that group, group by county.
Inside that group, group by town.
Inside that group, group by street.
Inside that group, group by name.

There are variations, of course, and it depends entirely on how you wish to see the data. If, for example, you wish to sort primarily on a person’s name (because you are trying to find a person by name), and then by address, you would put your first sort criterion by name.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool compare_by_name_then_address( const addressCard& a, const addressCard& b )
{
  if (a.name > b.name) return false;
  if (a.name < b.name) return true;

  if (a.postCode > b.postCode) return false;
  if (a.postCode < b.postCode) return true;
  
  if (a.county > b.county) return false;
  if (a.county < b.county) return true;

  ...

  return false;
}

For sorting the data primarily by zip, the function looks very much the same, just reorder the pairs in your comparison function.

Hope this helps.
Topic archived. No new replies allowed.