Click here to Skip to main content
Click here to Skip to main content

Callback based, Quicksort enabled CArray template class

, 18 Oct 2000
Rate this:
Please Sign up or sign in to vote.
This article presents a callback based, QuickSort enabled CArray template class.

Sample Image - ArrayEx.jpg


This article is based on Martin Ziacek's CQArray template class and Russell Robinson's COCArray template class. Please read their article before going on, I will mention the modifications only.


I'm using a lot of lists (typically listviews) in my projects, with a lot of records. If you ever tried to place about 200.000 rows into a listview, you must know it's a nightmare! Moving in the list is very-very slow, memory allocation is very high, and if you want to retrieve any data (because the user has double clicked the row, for example), you have to convert your string to a "usable" data type. The owner data lists are much faster, but if you like to implement a sortable owner data list, you have to deal with the sorting. That's why I needed this CArrayEx class.


Using CArrayEx is similar to using CArray but you have to specify only 1 parameter in the declaration:

typedef struct
    int   index;
    float fSomeFloat;
    TCHAR SomeString[128];
} UserType;

CArrayEx<UserType>    m_Data;

where UserType is a user defined struct.

For the sorting to be done, you have to declare a static member function in your class where you want the sorting to be done. For example, a CDocument derived class (in the case of a Doc/View based application). If you have the procedure, then you have to tell the array to use that procedure for item compare, and then call QuickSort by specifying the column number and sort direction.

The CDocument derived part will look like this:

(in the .h fileSmile | :)

static int CompareUserType(int iColumn, const UserType& udtLeft, 
              const UserType& udtRight);

(in the .cpp fileSmile | :)

int CMyDocument::CompareUserType(int iColumn, 
    const UserType& udtLeft, const UserType& udtRight)
    int iResult = 0;

    case 1:
        if (udtLeft.index==udtRight.index)
            iResult = 0;
        else if (udtLeft.index&ludtRight.index)
            iResult = -1;
        else iResult = 1;
    case 2:
        if (udtLeft.fSomeFloat==udtRight.fSomeFloat)
            iResult = 0;
        else if (udtLeft.fSomeFloat<udtRight.fSomeFloat)
            iResult = -1;
        else iResult = 1;
    case 3:
        iResult = strcmp((char*)udtLeft.SomeString, 

    return iResult;

void CMyDocument::DoSort()

    // Sort on index field of the structure, ascending direction
    m_Data.QuickSort(1, TRUE);

It's easy, isn't it?

Final words

In the sample program (which was originally from Martin's article), you can specify an item count, and the type of sorting on an int array and on a UserType array.

I hope this class can save some time for a few people.

There is no copyright and the (un)usual stuff. Smile | :)

Any improvements are welcome! Use this code free, but at your own risk.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Attila Hajdrik
Founder Eyedea Ltd.
Hungary Hungary
I'm a former Microsoft Senior Consultant who established his own Company at the beginning of 2008. Our Company is focusing on .Net development, mainly Silverlight 3.0 and WPF projects.
I was awarded as ASP.Net MVP back in 2004.

Comments and Discussions

GeneralMultiple sorting PinmemberIQ04-Sep-03 14:30 
GeneralRe: Multiple sorting PinmemberAttila Hajdrik5-Sep-03 8:45 
GeneralBug in Quicksort-Core PinmemberMichael Uebner23-Jan-01 20:58 
GeneralSTL vector PinsussAndy Hassall22-Oct-00 14:38 
GeneralUse virtual list. PinsussJet20-Oct-00 8:16 
GeneralRe: Use virtual list. PinsussAttila Hajdrik20-Oct-00 21:14 
GeneralCorrection PinsussAttila Hajdrik20-Oct-00 3:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150128.1 | Last Updated 19 Oct 2000
Article Copyright 2000 by Attila Hajdrik
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid