I want to sort elements in vector

myData class has int id, char* name;

And I push_back some myData class like this.

myData *a1 = new MyData(0, "a1");
myVector.push_back(a1);
myData *a2 = new MyData(1, "a2");
myVector.push_back(a2);
....

then, I want to arrange them by id like,
0->1->2->...

Initailly, I tried to use qsort or other sort algorithm provided with C++.

But I don't know how to use it with those class type.

How to do that??

Thanks.

Can't see your code...can't offer suggestion
1
2
3
4
5
6
7
8
9
10
#include <algorithm>

bool isLess(myData* a, myData* b)
{
//... return *a < *b if you have defined operator< for myData
}

//...
std::sort(myVector.begin(), myVector.end(), isLess);
//.. 


You need to define an comparation function because you have vector of pointers instead of vector of objects.
solved it!! Thanks All!!!
Topic archived. No new replies allowed.