Sorting STL list by custom comperator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • danalovesc
    New Member
    • Sep 2010
    • 9

    Sorting STL list by custom comperator

    Hey guys, i looked at some sorting examples, and i thought i jsut had to build a comperator which returns bool, in order to keep my list sorted by that value.

    here's the relevent code:

    Code:
    bool CityHall::EmployeeComp (Employee *e1,Employee *e2) const
    {
    if (e1->GetID()<e2->GetID())
    	return true;
    return false;
    }
    
    char** CityHall::PrintAllWorkersByLastName() const
    {
    	if (m_cityHallWorkers->size()==0)
    		 return NULL;
    	 m_cityHallWorkers->sort(EmployeeComp);
    .
    .
    .


    and i get:

    Error 1 error C3867: 'CityHall::Empl oyeeComp': function call missing argument list; use '&CityHall::Emp loyeeComp' to create a pointer to member.
    Error 2 error C2660: 'std::list<_Ty> ::sort' : function does not take 1 arguments.

    I know the comperator takes to args, but in all the examples they didn't send any, what should i send it if i should?!

    Thank you very much for ur help and time
    Last edited by Banfa; Sep 3 '10, 01:06 PM. Reason: Added code tags
  • hype261
    New Member
    • Apr 2010
    • 207

    #2
    Here is a page on the sort function for a list. The sort method can take a custom comparitor for sorting the list, but why would you? You sort your list by the < operator just like the default sorting method does. Why don't you just define the < operator for your Employee class?

    Comment

    • newb16
      Contributor
      • Jul 2008
      • 687

      #3
      I know the comperator takes to args.

      I don't get it. What does comparator takes to them? Anyway, the error message is quite clear - "use '&CityHall::Emp loyeeComp' to create a pointer to member." -
      will
      Code:
      m_cityHallWorkers->sort(&CityHall::EmployeeComp);
      work?

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Your problem is that you have tried to call a member function of CityHall. However to call a member function you need to have an instance of the class to call it on and there is no way to pass that instance into list::sort.

        There is a solution:

        Remove EmployeeComps dependency on having an object of type CityHall. This is easy make it a static member function which do not require an object to all and pass it to list::sort through the class CityHall::Emplo yeeComp . An alternative is to move the method out of the CityHall class entirely but then that puts it into global scope which is probably not good.

        Comment

        • danalovesc
          New Member
          • Sep 2010
          • 9

          #5
          hype261 --> I will try defining < for my class, i was just wondering why doesn't it work, and what should be the syntax for it to work

          newb16-->
          when u do that u get:


          Error 2 error C2064: term does not evaluate to a function taking 2 arguments

          and it jumps a file called "xutility" where it show the error in it

          Comment

          • danalovesc
            New Member
            • Sep 2010
            • 9

            #6
            @ Banfa --> how do i turn it into static, should i just add
            "static" before the func decleration at cityhall.h?

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Yes .

              Comment

              • danalovesc
                New Member
                • Sep 2010
                • 9

                #8
                and then i get:


                Error 1 error C2272: 'EmployeeComp' : modifiers not allowed on static member functions

                same code as before, just added static at the h file

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  Like I said you will need to access it through the class so

                  m_cityHallWorke rs->sort(CityHall: :EmployeeComp);

                  Comment

                  • danalovesc
                    New Member
                    • Sep 2010
                    • 9

                    #10
                    this is how it is written in the h file:
                    static bool EmployeeComp (Employee *e1,Employee* e2) const;

                    still gives the same error when i did:

                    m_cityHallWorke rs->sort(CityHall: :EmployeeComp);

                    Error 1 error C2272: 'EmployeeComp' : modifiers not allowed on static member functions

                    Comment

                    • newb16
                      Contributor
                      • Jul 2008
                      • 687

                      #11
                      remove const modifier.

                      Comment

                      • danalovesc
                        New Member
                        • Sep 2010
                        • 9

                        #12
                        well if only i knew where do i even modify a const here..

                        anyway i think i'll just go with definind < operator

                        thank you all!!

                        if someone can still point what the problem is i'll be happy to hear!:)

                        Comment

                        • hype261
                          New Member
                          • Apr 2010
                          • 207

                          #13
                          Remove the const at the end of this statement.

                          Code:
                          static bool EmployeeComp (Employee *e1,Employee* e2) const;

                          Comment

                          • danalovesc
                            New Member
                            • Sep 2010
                            • 9

                            #14
                            thnx alot, this works!

                            Comment

                            Working...