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;

// 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)

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

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


  • 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;

  • 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

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

    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.

  • 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?

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

Top White Papers and Webcasts

  • Protect Your Business against Today's Rapidly Growing Threats Your business, employees, partners, and customers are depending more and more on network connectivity, and protecting data and infrastructure has moved to the top of the IT agenda. You can't arm yourself against today's rapidly increasing threats unless you understand how they work. Read this ebook to learn more about the top 10 DNS attacks that can target your external and internal DNS infrastructure, the impact they can have on the DNS server …

  • Today's enterprise datacenter can be one of the most complex business environments with dozens (for smaller business), hundreds (for larger and midsize business), even thousands (for hyperscale businesses) of servers that must be managed and monitored. At this level, just managing the cords can be challenging — let alone keeping up with the growing need for more agility and scalability within the datacenter. Simply put, companies are aggressively looking for less complexity and more agility from their …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date