Sorting Vector of Structs in STL

This is a discussion on Sorting Vector of Structs in STL within the C++ Programming forums, part of the General Programming Boards category; Hey guys. Do any of you know a way to sort a vector of structs in STL? I tried using ...

  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,340
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    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
    21,914
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 01: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

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