QSort for CStringArray

Environment: Tested on VC5

These three routines can be used to perform a qsort on a CStringArray.  It is a fairly simple hack, but still a hack.  It sure is fast, though.


// first, declare these somewhere
void SortStringArray (CStringArray& ar, BOOL bDescending);
int CompareDescending(const void *a, const void *b);
int CompareAscending(const void *a, const void *b);

// put something in your CString array
  CStringArray bob;
  bob.Add("Are");
  bob.Add("You");
  bob.Add("Talking");
  bob.Add("To");
  bob.Add("Me?");

// sort it
  SortStringArray(bob, TRUE);

// here's the code!

//////////////////////////////////////////////////////////////////////////
int CompareAscending(const void *a, const void *b)
{
  CString *pA = (CString*)a;
  CString *pB = (CString*)b;
  return (pA->Compare(*pB));
}

//////////////////////////////////////////////////////////////////////////
int CompareDescending(const void *a, const void *b)
{
  CString *pA = (CString*)a;
  CString *pB = (CString*)b;
  return (-1 * (pA->Compare(*pB)));
}

//////////////////////////////////////////////////////////////////////////
void SortStringArray (CStringArray& csa, BOOL bDescending)
{
  int iArraySize = csa.GetSize();
  if (iArraySize <= 0)
     return;

  int iCSSize = sizeof (CString*);
  void* pArrayStart = (void *)&csa[0];

  if (bDescending)
     qsort (pArrayStart, iArraySize, iCSSize, CompareDescending);
  else
     qsort (pArrayStart, iArraySize, iCSSize, CompareAscending);
}



Comments

  • Case sensitivity added...

    Posted by Legacy on 01/26/2000 12:00am

    Originally posted by: Troels

    //////////////////////////////////////////////////////////////////////////
    inline int StringArray_Compare(const void* a, const void* b, BOOL bCaseSensitive)
    {
    LPCTSTR pA = ((CString*)a)->operator LPCTSTR();
    LPCTSTR pB = ((CString*)b)->operator LPCTSTR();
    return bCaseSensitive ? _tcscmp(pA, pB) : _tcsicmp(pA, pB);
    }
    static int __cdecl StringArray_CompareAscending (const void* a, const void* b)
    {
    return StringArray_Compare(a, b, FALSE);
    }
    static int __cdecl StringArray_CompareDescending (const void* a, const void* b)
    {
    return StringArray_Compare(a, b, FALSE) * -1;
    }
    static int __cdecl StringArray_CompareAscending_Case (const void* a, const void* b)
    {
    return StringArray_Compare(a, b, TRUE);
    }
    static int __cdecl StringArray_CompareDescending_Case(const void* a, const void* b)
    {
    return StringArray_Compare(a, b, TRUE) * -1;
    }
    //////////////////////////////////////////////////////////////////////////
    int StringArray_Sort(CStringArray* pas, BOOL bCaseSensitive, BOOL bAscending)
    {
    const int count = pas->GetSize();
    qsort(pas->GetData(), count, sizeof(CString*),
    bAscending ? (bCaseSensitive ? StringArray_CompareAscending_Case : StringArray_CompareAscending )
    : (bCaseSensitive ? StringArray_CompareDescending_Case : StringArray_CompareDescending));
    return count;
    }
    //////////////////////////////////////////////////////////////////////////

    Reply
  • 1. Case insensitive sorting and 2. missing parameter error is not an error

    Posted by Legacy on 07/27/1999 12:00am

    Originally posted by: Will Mainwaring

    1.
    
    To sort the array without case considerstions, replace
    "pA->Compare(*pB)" with
    "pA->CompareNoCase(*pB)"

    2.
    The Compare function is not missing parameters, as it is a function pointer. qsort uses elements of the array passed in as void parameters to the Compare...() function.

    The error that this guy has reported is probably due to declaring the 3 functions in his class definition:
    void CMyClass::SortStringArray(...)
    As they are C functions, not C++ functions, you must use:
    void SortStringArray(...)
    Check the help in VC on the "__cdecl" keyword for C -> C++ compatibility.

    Reply
  • Compare function is missing parameters

    Posted by Legacy on 07/07/1999 12:00am

    Originally posted by: Tim Hodgson

    Ex: qsort (pArrayStart, iArraySize, iCSSize, CompareDescending);

    CompareDescending is missing parameters to make this code work.

    Anyone know what should go there?

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds