how to sort CAtlArray using qsort

This is nothing new at all,here is the source code:

CAtlArray< CSomeClass*> m_myArray;
qsort(m_myArray.GetData(), m_myArray.GetCount(), sizeof(CSomeClass*),
  (int(*)(const void*, const void*))SortTheArray);

 

the Comparison function would look like the following:

int __cdecl SortTheArray(CSomeClass **pp1, CSomeClass **pp2)
{
    ....
}
Related posts:

TAGS: , ,


3 Comments »

Wang On February 1st, 2010 at 11:27 pm (#)

Good point. But what if CAtlArray contains structs instead of pointers? I am using
struct MyFile { CString sFilename; LARGE_INTEGER Lcn; DWORD dwFragments; };
CAtlArray<MyFile> asFiles;
After sorting, the CStrings are messed up, and once the array is cleaned up there is an access violation.

rong On September 21st, 2007 at 7:10 pm (#)

thank you..

Ivan On September 16th, 2007 at 10:07 pm (#)

Yes It is works! Thank you very much!


Leave a comment


(will not be published)