Sorting a list of people by a given attribute

This is a discussion on Sorting a list of people by a given attribute within the C++ Programming forums, part of the General Programming Boards category; I need help in writing two classes, Person and Personlists, where I am supposed to gather the attributes of persons ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    102

    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;
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.).

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    Quote Originally Posted by tabstop View Post
    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
    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 View Post
    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.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,526
    "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.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    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?

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,526
    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.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    2,144
    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; });
    Last edited by Elkvis; 06-06-2011 at 11:55 AM. Reason: missed a semicolon

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,012
    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);
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 08-25-2008, 11:08 PM
  2. Sorting a list
    By jw232 in forum C++ Programming
    Replies: 3
    Last Post: 05-14-2008, 08:24 PM
  3. help with sorting a list :(
    By Axel in forum C Programming
    Replies: 54
    Last Post: 10-16-2006, 07:10 AM
  4. How many people are on your buddy list?
    By Salem in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 11-26-2004, 10:14 PM
  5. IT people - weird genius or simply narrow-minded, antisocial, lazy people
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-11-2001, 05:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21