How to use array sort ?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.

How to use array sort ?

Postby sodandk » Mon Jun 11, 2012 12:50 pm

I have an array of
core::array<MY_Obj*> objects;

then I do
objects.sort();

and inside the MY_Obj I have:

bool operator<(const MY_Obj* a) const
{
return distance < a->distance;
};

but the sort operator never gets called ?
User avatar
sodandk
 
Posts: 136
Joined: Wed Aug 10, 2011 11:58 am

Re: How to use array sort ?

Postby CuteAlien » Mon Jun 11, 2012 3:30 pm

The problem is that you have MY_Obj* and not MY_Obj. So the operator is never called as the type is not of the class MY_Obj but a pointer. So you are sorting by memory-address.

In short - use stl::vector in combination with std::sort instead of Irrlicht array, that allows passing functors. Irrlicht is still missing this.
IRC: #irrlicht on irc.freenode.net
My patches&stuff: http://www.michaelzeilfelder.de/irrlicht.htm
Games with Irrlicht: http://www.irrgheist.com/
News: http://www.reddit.com/r/irrlicht/
User avatar
CuteAlien
Admin
 
Posts: 5364
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany

Re: How to use array sort ?

Postby sodandk » Mon Jun 11, 2012 3:38 pm

oh shoots... was converting all vectors to arrays ...
User avatar
sodandk
 
Posts: 136
Joined: Wed Aug 10, 2011 11:58 am

Re: How to use array sort ?

Postby CuteAlien » Mon Jun 11, 2012 3:43 pm

Don't do that in your own application. My recommendation would be to use irrlicht core classes only when you are coding inside Irrlicht and otherwise to avoid them completely and use STL.
IRC: #irrlicht on irc.freenode.net
My patches&stuff: http://www.michaelzeilfelder.de/irrlicht.htm
Games with Irrlicht: http://www.irrgheist.com/
News: http://www.reddit.com/r/irrlicht/
User avatar
CuteAlien
Admin
 
Posts: 5364
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany

Re: How to use array sort ?

Postby sodandk » Mon Jun 11, 2012 4:06 pm

why ?
User avatar
sodandk
 
Posts: 136
Joined: Wed Aug 10, 2011 11:58 am

Re: How to use array sort ?

Postby CuteAlien » Mon Jun 11, 2012 4:52 pm

Depends on the applications. But in general STL is just more powerful, faster and more stable. Irrlicht core classes offer more or less the stuff needed inside the engine, while STL offers pretty much everything you need in a typical application.
IRC: #irrlicht on irc.freenode.net
My patches&stuff: http://www.michaelzeilfelder.de/irrlicht.htm
Games with Irrlicht: http://www.irrgheist.com/
News: http://www.reddit.com/r/irrlicht/
User avatar
CuteAlien
Admin
 
Posts: 5364
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany


Return to Beginners Help

Who is online

Users browsing this forum: No registered users and 1 guest

cron