Sign in to follow this  

Sorting a vector of objects based on a member of that object...

This topic is 3037 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How can I do this? I found a few things on google and tried them but I kept getting compiler errors. I have a class that looks like this: class Word { private: std::string word; }; I want to sort by word.length() in descending order. I tried something like: bool predicateSort(const Word& w1, const Word& w2) { return w1.word.length() < w2.word.length() } std::sort(myVector.begin(), myVector.end(), predicateSort); And I tried a few other variations of this such as overloading the operator < in the class so it'd look like: bool operator<(MyData rhs) { return m_iData < rhs.m_iData; } I mainly tried the stuff listed here: http://www.codeguru.com/forum/showthread.php?t=366064 Thanks for the help.

Share this post


Link to post
Share on other sites
What's the type of myVector? If it's a std::vector<Word> then your predicateSort should have worked (aside from missing a ; in the predicateSort() definition and Word's word member being private).

Share this post


Link to post
Share on other sites
It's type Word, yeah. It keeps pointing me to stl_algo.h saying I have some error.

The class has this as a public method:
bool operator<(Word w) { return word.length() < w.word.length(); }

Then I try to use:
std::vector<Word> playerWords = player1.getWordList();
std::sort(playerWords.begin(), playerWords.end());

Never done this before so I'm probably doing something real dumb. I get lots of errors like:

C:\Program Files (x86)\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\stl_algo.h||In function `const _Tp& std::__median(const _Tp&, const _Tp&, const _Tp&) [with _Tp = Word]':|

C:\Program Files (x86)\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\stl_algo.h|2482|instantiated from `void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Word*, std::vector<Word, std::allocator<Word> > >, _Size = int]'|

C:\Program Files (x86)\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\stl_algo.h|90|error: passing `const Word' as `this' argument of `bool Word::operator<(Word)' discards qualifiers|

Share this post


Link to post
Share on other sites

This topic is 3037 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this