Skip to main content
gamedev.net
🔒 Locked

C++ vector sorting

Started by trixs Jan 16, 2008 at 1:07 PM 8 replies 1.2k views
Original Post
trixs
trixs
Hey All, Im trying to figure out how to sort a vector, list ect that contains an object. I want to sort by a variable inside the object. Ive searched for hours and cant seem to find an actual example. Google is full of simplistic sorting numbers tho! Lets say your vector is full of: class test { public: int a; int b; int c; } you want to sort by int b - or whatever.. How would you do it? Thanks
DeadlyDan
DeadlyDan
Look into iterators, you'll find that's what you want.

This website should be helpful:

http://www.mihaiu.name/2002/stl_container_vector/
trixs
trixs
Thanks for the prompt reply.

Unfortunately that site simply shows how to sort numbers - just like every example I have found... Is there even away to sort by what is in the objects? Yes, I could iterate all the way through but thats slow.
rolkA
rolkA
Provide the < operator to your class, then use std::sort
English is not my native language.Sam.
stonemetal
stonemetal
all stl containers have a sort function either built in or in the algorithms header. the sort function takes a comparison function as the second argument.
trixs
trixs
Yea, understand the theory of how it is supposed to work..

However, can not find an example of the syntax which is what I dont understand..

Heres an example of what is on various sites::

int myints[] = {32,71,12,45,26,80,53,33};
vector myvector (myints, myints+8); // 32 71 12 45 26 80 53 33
vector::iterator it;

// using default comparison (operator <):
sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33

// using function as comp
sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)


Wonderful.. Except no example of sorting objects.
smitty1276
smitty1276
You just have to write a function that says whether the first is less than, equal to, or greater than the second one. What, specifically, is giving you trouble? Ultimately, you are just sorting ints, because test.b is an int.
rip-off
rip-off
Example of custom sorting functor:

struct test {   int a;   int b;   int c;};std::ostream &operator <<( std::ostream &out, const test &t ){    out << "{ " << t.a << ' ' << t.b << ' ' << t.c << " }";    return out;}struct SortByA {    bool operator()( const test &one, const test &two ) const    {         return one.a < two.a;    }};struct SortByB {    bool operator()( const test &one, const test &two ) const    {         return one.b < two.b;    }};int main(){    std::vector<test> v;    for( int i = 0 ; i < 5 ; ++i )    {        test t = {rand()%10,rand()%10,rand()%10};        v.push_back(t);    }    std::sort(v.begin(),v.end(),SortByA());    std::copy(v.begin(),v.end(),std::ostream_iterator<test>(std::cout, " "));    std::cout << '\n';    std::sort(v.begin(),v.end(),SortByB());    std::copy(v.begin(),v.end(),std::ostream_iterator<test>(std::cout, " "));    std::cout << '\n';}
stonemetal
stonemetal
Quote:
Original post by rolkA
Quote:
Original post by stonemetal
all stl containers have a sort function either built in or in the algorithms header. the sort function takes a comparison function as the second argument.

Not really, you can provide a predicate but if you dont, then < is used. And containers do not have any embeded sort function, you have to use std::sort. (try to use v.sort() with v being a vector then come back...).



Here is An example that illustrates what i said earlier :
*** Source Snippet Removed ***


so you are saying list.sort doesn't exist, interesting.
the predicate you will want to write is
bool sortstuff(element1, element2)
{return true if 1 should come before 2}
then you call sort(v.begin,v.end,sortstuff)
trixs
trixs
Thank you for the code snippits - exactly what I needed. Hopefully this thread gets indexed by google for some other helpless newb!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.