Solved

Sorting a CPtrArray: problems!

Posted on 2008-06-17
4
821 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
Comment Utility
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
Comment Utility
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
Comment Utility
Both of those changes were required for it to work, so points split!

Thanks both :o)
0
 
LVL 19

Author Comment

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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now