Thread: sorting container

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    sorting container

    Hello

    I have a ptr_container:

    Code:
    ptr_vector<base> container;
    In container I store derived objects from base.

    Code:
    class derived1 : public base {
    };
    class derived2 : public base {
    public:
       int number;
    };
    Derived2 class has an int member and I'd like to sort elements in container by the highest number.

    I know if I had normal container I could do:
    Code:
    std::sort(container.begin(), container.end(),
        tr1::bind( .. etc..
    But how can I do it in this case? What is the best way? I dont want to get things too complicated if not neccessary.

    Note that classes contain enum so I know which type they are (so I can do static_cast) and only derived2 should be sorted in container. There could be also derived or derived3 objects in a container.

    Any help is highly appreciated

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    I don't know about vectors, but for std::map, you can supply your own key comparison function.
    Have a look here for an example of how to write one.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    630
    The problem is I have a pointer container (not a usual container).

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    But how can I do it in this case? What is the best way? I dont want to get things too complicated if not neccessary.
    I am not sure about an in place fix, but as in QuantumPete's link to a comparator, you could write such a comparator that dereferenced and compared elements and pass that comparator to std::sort().

    Note that classes contain enum so I know which type they are (so I can do static_cast) and only derived2 should be sorted in container. There could be also derived or derived3 objects in a container.
    If only derived2 should be sorted... then why store base pointers in the first place? I have this nagging feeling that something is really wrong with your class design.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    630
    Its not my design.. Its a 'parsing' library which I have to use and after data is parsed I have to sort the results..

    However, I could copy the data in my own container and simplify the design. I dont know which option would be better.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    So, the container holds a mixture of Derived1 and Derived2, and only the Derived2 elements should be sorted? Then where do the Derived1 elements go? You cannot sort unless you have a total order.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    A generic dereferencing function always comes in handy...

    Code:
    template <typename T>
    T &deref(T *ptr)
    {
        return *ptr;
    }
    Once you have that, a few binds do the trick:

    Code:
    std::sort(x.begin(), x.end(), bind(std::less<int>(), bind(deref<int>, _1), bind(deref<int>, _2)));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. sorting container
    By l2u in forum C++ Programming
    Replies: 13
    Last Post: 05-04-2007, 05:01 AM
  3. Linked List Queue Implementation help
    By Kenogu Labz in forum C++ Programming
    Replies: 8
    Last Post: 09-21-2005, 10:14 AM
  4. Map Container & Sorting :: STL
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 09-09-2002, 06:01 PM
  5. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM