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


  • There are no comments yet. Be the first to comment!

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

Top White Papers and Webcasts

  • IDC estimates that more than 70% of organizations have a cloud-first application deployment strategy to align with digital transformation initiatives. As such, many organizations are transitioning to Microsoft Office 365 and utilizing public clouds such as Microsoft Azure. However, IT leaders are discovering that default data protection schemes are not as robust as they desire. As a result, many are turning to Veeam® to provide the necessary data management and protection policies demanded by the …

  • The software-defined WAN (SD-WAN) has emerged as the solution for enterprises embracing cloud computing as a means to realizing digital transformation. In this IDC brief, learn why the need for SD-WAN will be even greater than the need for SDN in the data center, especially when an organization's application profile is migrating toward public cloud services and where customer engagement is most valued.

Most Popular Programming Stories

More for Developers

RSS Feeds

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