Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Sorting a STL list?

Author
Message
McLaine
18
Years of Service
User Offline
Joined: 20th Feb 2006
Location:
Posted: 7th Feb 2012 15:51
I understand that STL Lists have a built in sort function.

However, If my list is of this example struct:

struct Player
{
string name;
int score;
}

What would be the best way to sort the list based on score?

It's not my fault!
WLGfx
16
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 7th Feb 2012 19:50
If you're using a vector then it's a simple as any old sort function.


EDIT: Nah that's wrong... Sowwy man... Needs a second loop inside... But it gives you an idea...

Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!
Matty H
15
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 7th Feb 2012 20:20
Have you looked here:
list::sort

Seems you write your own 'compare' function which will take two of your objects and return true or false. It's up to you what attribute you use to compare, 'score' in your case.

I only looked at it briefly but I have done compare functions in Java, I am presuming this is the same, but I may be wrong.

Dar13
15
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 7th Feb 2012 23:56
What MattyH told is exactly what you do. You create a function that compares two given instances of that struct and return either true or false depending on which one is greater than the other.

McLaine
18
Years of Service
User Offline
Joined: 20th Feb 2006
Location:
Posted: 8th Feb 2012 01:49 Edited at: 8th Feb 2012 01:57
Cheers for the effort WLGfx, I got what you were getting at. That's a standard bubble sort which I was going to fall back on. It's not far from the actual implementation, but I was looking to use the in built sort method as I assume that's going to be faster.

@Matty H and Dar13
Coolio, thanks for the link. I tried to find that in the C++ manual but failed, I have no idea why. Maybe I was just looking at the top level of list.

So, build a method that takes two parameters of the type of my struct, and that returns true or false. Then pass that method name to the sort method?

Nice one fellas!

It's not my fault!
Dar13
15
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 9th Feb 2012 04:26
Quote: "Then pass that method name to the sort method?"

Yes, but by doing so you're actually passing a pointer to the function(aka a function pointer) not the function name.

Login to post a reply

Server time is: 2024-03-29 14:18:18
Your offset time is: 2024-03-29 14:18:18