sorting vector in function

hi guys i'm new to C++ especially vectors..
I've a question regarding sorting of object in a vector

my object consists of x,y and civ. so I would like to know how do I sort the value of the civ in an descending order but at the same time retaining the value of x and y attached to it..

1
2
3
4
5
6
7
8
9
original
X: 4 Y: 5 CIV: 10
X: 3 Y: 2 CIV: 30
X: 3 Y: 3 CIV: 05

sorted
X: 3 Y: 2 CIV: 30
X: 4 Y: 5 CIV: 10
X: 3 Y: 3 CIV: 05


missionplan.cpp
1
2
3
4
5
6
void MissionPlan::topfives()
{
  stable_sort (topfive.begin(), topfive.end());
	pointtwoD = topfive.at(i);
	pointtwoD.displayPointdata();
}
Use a custom comparator:
1
2
3
4
std::sort(std::begin(topfive), std::end(topfive), [](YourObject const &a, YourObject const &b) -> bool
{
    return a.civ < b.civ;
});
Last edited on
Hi L B, thanks..

but I have another problem, correct me if i'm wrong.

There's is no need to declare "sort", "begin" and "end" because they are a function provided by vector. But whenever i compile I will have a error msg saying that " 'begin' was not stated in the scope.

another que is that my civIndex is actually a private data in my PointTwoDImp.cpp.
Topic archived. No new replies allowed.