sorting

guys, how do i do sorting in a stl linked list?

for example in my link list there is a (float x) and there are a lot of them.

how do i cout the x from biggest to smallest?
closed account (D80DSL3A)
http://www.cplusplus.com/reference/stl/list/sort/
Use STL sort followed by STL copy with ostream iterator.
actually i dont really understand what the link taught...

can anyone explain to me in a simpler term...? sorry for the inconvenience caused.

actually i just wanna remove away duplicate entries and wanna display the top 5 and ignore the others.
Last edited on
to give a clearer picture.. this is my coding..


void MissionPlan::TopFive(list<PointTwoD>clist) {

list <PointTwoD>::iterator i;
sort(clist.begin(), clist.end());

do{
for (i = clist.begin(); i != clist.end(); i++) {

cout << "Civ Idx : " << ld.computeCivIndex(i->Ld.getSunType(), i->Ld.getNoPlanets(), i->Ld.getNoMoons(), i->Ld.getAveParticulateDensity(), i->Ld.getAvePlasmaDensity()) << " at sector(" << i->getX() << "," << i->getY() << ")" << endl;

}
}while(i==4);

}


i wanna do a sorting in my link list first... arranging them in the manner according to the civIndex. the cixIndex is computed in another function and it can be successfully computed(tested). but now i have no idea how to sort them from biggest to smallest and only display the top 5 in my stl linked list..
bumpz

 
void MissionPlan::TopFive(list<PointTwoD>clist)

This line above makes a copy of the list that is passed in. So when you leave the scope of the function the sorted list is lost.

to make a point this line would look like this:

The line below will use the list that is passed in and not make a copy of it. Assuming you wanted the list you were passing in to be sorted. This will keep the sorted list associated with the one you passed in.
 
void MissionPlan::TopFive(list<PointTwoD> &clist)


I am not sure this is exactly what you needed but it would be the one change I would do.
actually the list were not sorted b4.. i wanna sort the list in this function and display only the top 5 result and it is sorted according to the cixIndex which can be retrieve from ld.computeCixIndex from biggest to smallest
Last edited on
First off, I don't think you know what a Bubble sort or Quick sort is. These are algorithms. The bubble is the easiest. The STL mess has some code hic-ups that might be a little complex for someone who doesn't understand the basics of a sort. You Know what you want as a starting point but you are not completely accurate on what you need for it.

From the code piece you supplied you have to code the conditional of the sort, which is the sort criteria. This is true even if you use the sort algorithms of the STL. You are using an object, which isn't a straight number sort for the sorting algorithms of the STL. The Object suggest you have more than one piece to the sorting criteria. I don't know the definition of PointTwoD, it suggest that it is more complex than a simple number sort.

The other thing you are asking for is two things: first sort them, then eliminate duplicates. They are two distinct thought processes. The Standard Template Library has things to handle both of these, but yet again I would have to code the sorting criteria and what constitutes a duplicate for the Object.

The Book "The c++ standard Library: A tutorial and Reference," by Nicolai M, Josuttis, is a great book to have laying around, If you want to use the STL. Otherwise you have to learn how to code this stuff by hand and you are missing the basics of the ideas you are trying to accomplish.

Cheers
Azagaros

Last edited on
yea... u are right.. i had no idea how what is the two sort.. the pointTwoD is a class that contain some infomation that pass in by the user...

and thanks for your suggestion
Topic archived. No new replies allowed.