Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ MFC STL Sorting Sort
Hi,
I have a vector of structures.
Struct Task
{
int field1;
int field2;
};
vector 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 21: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
pwasser at 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 at 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
0 OriginalGriff 341
1 Marcin Kozub 225
2 Praneet Nadkar 197
3 Sergey Alexandrovich Kryukov 190
4 Shweta N Mishra 161
0 OriginalGriff 8,149
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 16 Jun 2012
Copyright © CodeProject, 1999-2014
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