# Sorting a list of people by a given attribute

• 06-05-2011
Dontgiveup
Sorting a list of people by a given attribute
I need help in writing two classes, Person and Personlists, where I am supposed to gather the attributes of persons and then allow them to be sorted by a given attribute, printing out the result. Here is what I have done so far:

Code:

```class Person {             int height;             int weight;             int age;             int IQ;             public:                         Person (int height = 0, int weight = 0, int age = 0, int IQ = 0) {                                         this->height = height;                                         this->weight = weight;                                         this->age = age;                                         this->IQ = IQ;                                         }                                         void find( int attribute = 0) ; //Thinking of creating a method //that allows me to sort by a given attribute but am stuck void print() {                                                 cout<<height;                                                 cout<<weight;                                                 cout<<age;                                                 cout<<IQ;                                                 } }; class Personlists { //This class too I am lost for ideas as to what I can do with it }; int main() { class Person p1(179, 75, 30, 100); p1.print(); return 0; }```
• 06-05-2011
tabstop
You will definitely need to follow through on "creating a method that allows [you] to sort by a given attribute". Think about how < works now -- it returns true or false, depending on whether the first argument comes "first" or not. So write a function that will do that for (say) age.

What you need to do with personlist depends on the parameters of your assignment (i.e., are you supposed to have things sorted at all times, how do you specify what to sort on, etc.).
• 06-05-2011
Dontgiveup
Quote:

Originally Posted by tabstop
You will definitely need to follow through on "creating a method that allows [you] to sort by a given attribute". Think about how < works now -- it returns true or false, depending on whether the first argument comes "first" or not. So write a function that will do that for (say) age.

Thanks but I did not get what you meant by
Quote:

Think about how < works now -- it returns true or false, depending on whether the first argument comes "first" or not. So write a function that will do that for (say) age.
.

Quote:

Originally Posted by tabstop
What you need to do with personlist depends on the parameters of your assignment (i.e., are you supposed to have things sorted at all times, how do you specify what to sort on, etc.).

Yeah, even me I do not know it as it wasn't explained. All that was said was "create two classes that allow the gathering of these data (the ones I wrote in the program already) and allow the printing out of the persons using any attribute chosen by the user.
• 06-05-2011
whiteflags
"Chosen by the user" means that if you gave me a finished program, I would be able to pick which part of a person by which I wanted to order my list. That means you have methods to sort by name, age, weight, height or IQ. Then you would write an algorithm that could call any one of those to actually do the ordering part.
• 06-05-2011
Dontgiveup
Oh, so you mean I have to create 5 methods, for name, age, weight and height and something like

Code:

```void checkage(int age = 0) { if(this->age == age) //then I don't know what to do } //and so on for the remaining four?```
• 06-05-2011
whiteflags
More like you want to order the parts of a list. This is why tabstop said think about how less-than works, because overloading less-than (and using std::sort) is a good way to sort. Even if you can't do that for the assignment, the particulars would be the same:

Code:

```bool SortByAge (const Person &a, const Person &b) {   return b.getAge() < a.getAge(); }```
Your sorting algorithm might be built so that it orders Person B before A if this is true. And you would do something similar for all the parts of Person.
• 06-06-2011
Elkvis
another possibility (if you have a compiler that supports it - VC++ 10, GCC >= 4.3) you could use std::sort and a lambda.

Code:

```struct MyStruct {   int foo;   std::string bar; } std::vector<MyStruct> vMyStruct; std::sort(vMyStruct.begin(), vMyStruct.end(), [](const MyStruct& lhs, const MyStruct& rhs){ return lhs.bar < rhs.bar; });```
• 06-06-2011
King Mir
Lamda's arn't in the standard yet, so your instructor may object to you using them, but you can use a function like whiteflags wrote. And for this particular application, I recommend stable_sort.
Code:

`std::stable_sort(vMyStruct.begin(), vMyStruct.end(), SortByAge);`