Solved

Sorting a CPtrArray: problems!

Posted on 2008-06-17
4
844 Views
Last Modified: 2013-11-20
Ah hello.

I am trying to sort a CPtrArray of my own simple class.  This is based on the MSDN article at http://support.microsoft.com/kb/q216858/.  Code follows:

typedef int (__cdecl *GENERICCOMPAREFN)(const void * elem1, const void * elem2);
typedef int (__cdecl *STRINGCOMPAREFN)(const CMyClass * elem1, const CMyClass * elem2);

class CMyArray : public CPtrArray
{
public:
      void Sort(GENERICCOMPAREFN pfnCompare = Compare)
      {
            qsort ( this, GetSize(), sizeof ( CMyClass ), ( GENERICCOMPAREFN ) pfnCompare );
      }
protected:

      static int __cdecl Compare(const void * pMyClass1, const void * pMyClass2 )
      {
            if ( !pMyClass1 || !pMyClass2 ) return 0;
            CMyClass* pActualMyClass1 = (CMyClass*)pMyClass1;
            CMyClass* pActualMyClass2 = (CMyClass*)pMyClass2;

            return pActualMyClass1 ->m_dw < pActualMyClass2 ->m_dw;
      }
};

int main()
{
      CMyArray m_array;

      m_array.Add ( ( void*) new CMyClass ( 1 ) );
      m_array.Add ( ( void*) new CMyClass ( 3 ) );
      m_array.Add ( ( void*) new CMyClass ( 2 ) );

      m_array.Sort();

      return 0;
}

I would like to sort my array by the CMyClass object's DWORD member, in ascending order.

I am finding however that when Compare gets called, the DWORD value of the CMyClass object is like over 3386696!  Not what I initialised the object with when I added it to the array!  

Can someone explain what I am doing wrong here, and hence how to sort this array?
0
Comment
Question by:mrwad99
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 63 total points
ID: 21804349
IIRC that should be

            CMyClass* pActualMyClass1 = *(CMyClass**)pMyClass1;
            CMyClass* pActualMyClass2 = *(CMyClass**)pMyClass2;

since the compare function gets called with an additional indirection.
0
 
LVL 19

Assisted Solution

by:alb66
alb66 earned 62 total points
ID: 21804499
You pass "this" instead of GetData();


      void Sort(GENERICCOMPAREFN pfnCompare = Compare)
      {
            void* p = GetData();
            if ( p )      
                qsort ( p, GetSize(), sizeof ( CMyClass ), ( GENERICCOMPAREFN ) pfnCompare );
      }
0
 
LVL 19

Author Comment

by:mrwad99
ID: 21811133
Both of those changes were required for it to work, so points split!

Thanks both :o)
0
 
LVL 19

Author Comment

by:mrwad99
ID: 21813449
Follow up at http:Q_23495395.html if anyone can help :)
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question