Click here to Skip to main content
13,554,127 members
Rate this:
 
Please Sign up or sign in to vote.
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:
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());


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 15-Jun-12 20:21pm

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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);
  Permalink  
v2
Comments
User-59241 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?
wang-chung-1970 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web01 | 2.8.180515.1 | Last Updated 16 Jun 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100