Click here to Skip to main content
15,881,755 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
Hi,
I have a vector of structures.
Struct Task
{
int field1;
int field2;
};
vector <task> vec;

How can i sort the vector based on "field1" ?
I used the following code for sorting:
C#
bool sortByField1(Task *t1, Task *t2)
{
    if(t1->field1> t2->field1)
    {
        return true;
    }
    else
        return false;
}


and called
C#
std::sort(vec.begin(), vec.end(),
          sortByField1());


I get error saying sortByField1() does not take 0 param.

I googled and i did not get anything that could help my case.
Please provide your inputs.
Posted

1 solution

C#
bool sortByField1(Task &t1, Task &t2)
{
    if(t1->field1> t2->field1)
    {
        return true;
    }
    else
        return false;
}
and called

std::sort(vec.begin(), vec.end(),
          sortByField1);
 
Share this answer
 
v2
Comments
[no name] 16-Jun-12 6:31am    
So you post a question. An hour later you post the answer and then accept your own answer. Cute.

Why not just spend the extra hour before asking the question?
wangChung1970 16-Jul-13 15:11pm    
Because somebody might have this exact same question some day....like me, now!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900