I have a token class with parameters are below-
private:
string theText{};
vector<size_t> theLineNumbers{};
size_t theFrequency{ 1 };
Now i have an other Tokenizer class which has a list of objects of token class
private:
list <Token> theTokenList;
Now i want to implement the function that sort the list of objects according to the frequency. I have written the below code but it's not working.
void Tokenizer::sortOnFrequecy()const
{
forward_list<Token> myflist(theTokenList.cbegin(), theTokenList.cend());
/*forward_list <Token>::const_iterator it = myflist.cbegin();*/
sort(myflist.cbegin(), myflist.cend(),compareFrequency());
}
bool Tokenizer::compareFrequency(const Token& t1, const Token& t2)
{
return (t1.getFrequency() < t2.getFrequency());
}
I want to print the list of token, sorted according to the frequency in decreasing order.
Please help me to solve this problem. Incase any other code is need, then i am more than happy to provide the same.
Check out this:
https://www.cplusplus.com/reference/list/list/sort/
You can either make the
compareFrequencystatic or move it out of the object entirely. Also you are calling this comparison without any arguments, that does not make sense.Making the
compareFrequencya normal function declared outside the object, sorting the list would be just