There are probably a lot of examples on sorting to find all around the internet. However I'd like to keep things within irrlicht first to understand entirely the engine.
The sorted should be done as follows:Code: Select all
// truncated class class cCharacter { public: ... struct sProperties { irr::core::stringw _name_first; irr::core::stringw _name_last; irr::s32 _age_in_days; // just a unixtime style date irr::s32 _date_of_birth; // just a unixtime style date ... }; /* protected: <-- inconvenient for now */ class cApplication* _application; cCharacterManager* _manager; irr::s32 _identifier; // unique identifier of this class withhin the array /* DATA TO SORT WITHIN ITS CONTAINER -> cCharacterManager::_characters */ sProperties _properties; sProperties _properties_old; // track what has changed }; typedef irr::core::array<class cCharacter*> cCharacterArray; class cCharacterManager { protected: cCharacterArray _characters; }
_name_last
or...
_date_of_birth
or...
_name_last; then _date_of_birth
whatever, you'll get it.
Having copied the heapsort and heapsink templates into a convenient place and renamed them; templates removed (as for now).
and calling it:irrlicht wrote:Code: Select all
//! Sinks an element into the heap. void charsink( cCharacter** array, irr::s32 element, irr::s32 max ) { while( ( element << 1 ) < max ) // there is a left child { s32 j = ( element << 1 ); if( j + 1 < max && array[ j ] < array[ j + 1 ] ) j = j + 1; // take right child if( array[ element ] < array[ j ] ) { cCharacter* t = array[ j ]; // swap elements array[ j ] = array[ element ]; array[ element ] = t; element = j; } else return; } } //! Sorts an array with size 'size' using heapsort. inline void charsort( cCharacter** array_, s32 size ) { // for heapsink we pretent this is not c++, where // arrays start with index 0. So we decrease the array pointer, // the maximum always +2 and the element always +1 cCharacter** virtualArray = array_ - 1; s32 virtualSize = size + 2; s32 i; // build heap for( i = ( ( size - 1 ) / 2 ); i >= 0; --i ) charsink( virtualArray, i + 1, virtualSize - 1 ); // sort array, leave out the last element (0) for( i = size - 1; i>0; --i ) { cCharacter* t = array_[ 0 ]; array_[ 0 ] = array_[ i ]; array_[ i ] = t; charsink( virtualArray, 1, i + 1 ); } }
The outcome is, ofcourse, the same as calling: _characters.sort();Code: Select all
cCharacter** data = _characters.pointer(); sortChar( data, sizeof( cCharacter ) * _characters.size() );
How to find the offset to (*(cCharacter**))->_properties and then those members?
r,