Thread: Sorting Vector of Structs in STL

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    34

    Sorting Vector of Structs in STL

    Hey guys. Do any of you know a way to sort a vector of structs in STL? I tried using a comparator such as

    Code:
    class compare
    {
       bool operator(structtype st1, structtype st2)
       {
            return st1.item < st2.item;
        }
    };
    
    then I do sort(vecofstructypes.begin(), vecofstructtypes.end(), compare());
    
    any ideas on why this would not work?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    How did it not work? Did you get a compile error? Did it not sort the items? What went wrong?

    The operator that the comparator uses is operator(), but you are missing the extra parentheses in yours. It should be operator()(structtype st1, structtype st2), or better yet:
    Code:
    bool operator()(const structtype& st1, const structtype& st2) const

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    First, the operator has to be public, second, the syntax is wrong, third, you should pass by const reference, and fourth, the whole method should be const:

    Code:
    public:
    bool operator()(const structtype &st1, const structtype &st2) const
    {
        return st1.item < st2.item;
    }

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    and 5. you have to use a pointer instead of a function call:
    Code:
    sort(vecofstructypes.begin(), vecofstructtypes.end(), compare);

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    and 5. you have to use a pointer instead of a function call:
    No, you cannot, since compare is a function object. I think brewbuck has got it all corrected.
    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

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    uh sorry, you are right!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting an Array of Structs not working
    By ashcan1979 in forum C++ Programming
    Replies: 9
    Last Post: 09-28-2006, 03:07 PM
  2. Can I use STL qsort() for sorting structure?
    By franziss in forum C++ Programming
    Replies: 14
    Last Post: 09-20-2005, 05:34 AM
  3. sorting containers of structs
    By bigSteve in forum C++ Programming
    Replies: 2
    Last Post: 01-06-2004, 02:50 PM
  4. Map Container & Sorting :: STL
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 09-09-2002, 06:01 PM
  5. STL list::sort() sorting structs
    By hk_mp5kpdw in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2002, 07:23 AM