Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

How to sort elements of RArray and RPointerArray

From Wiki
Jump to: navigation, search
Article Metadata
Created: savaj (10 Mar 2009)
Last edited: hamishwillee (03 Dec 2012)
Objects stored in RArray or RPointer array can be easily sorted inplace based on a custom ordering algorithm. To achieve this developer needs to define a custom ordering function that compares two instances of the stored objects and returns an integer based on the comparison. To sort a RArray/RPointerArray simply pass this function to the RArray/RPointerArray::Sort function by packaging it in a TLinearOrder object. Following example shows how to sort RPointerArray, similarly we can sort RArray.
//declaration of class.
class CPerson: public CBase
// other functions are removed for sake of simplicity.
/** Sort Order By Name**/
static const TLinearOrder<CPerson> KSortOrderByName;
/**Comparator Function for sort by name**/
static TInt CompareByName( const CPerson& aNameOne,const CPerson& aNameTwo );
/**instance data*/
HBufC* iName;
/**initialize the sort order to use CPerson::CompareByName*/
const TLinearOrder<CPerson> CPerson::KSortOrderByName(CPerson::CompareByName);
/**comparision function, it compare two names.*/
TInt CPerson::CompareByName( const CPerson& aNameOne, const CPerson& aNameTwo )
return aNameOne.iName->Des().CompareC( aNameTwo.iName->Des() );
//the pointer array in to be sorted
RPointerArray<CPerson> elementArray;
//assuming we created all objects of class CPerson sucessfully.
//sorting elementArray by name.
This page was last modified on 3 December 2012, at 23:48.
17 page views in the last 30 days.