How to Sort a CStringArray in MFC

Last reviewed: October 10, 1997
Article ID: Q120961
7.00 | 1.00 1.50 | 1.00 2.00 2.10 4.00
MS-DOS | WINDOWS   | WINDOWS NT
kbprg kbcode

The information in this article applies to:

  • The Microsoft Foundation Classes (MFC) included with:

        - Microsoft C/C++ version 7.0
        - Microsoft Visual C++ for Windows, versions 1.0 and 1.5
        - Microsoft Visual C++, 32-bit Edition, versions 1.0, 2.0, 2.1, and
          4.0
    

SUMMARY

You can use the sample code in this article to sort a CStringArray object. The main() function constructs a CStringArray object, adds elements to it, prints out the elements, calls the sort() member function to sort it, and then prints the sorted elements. The sort() function uses the Bubble Sort algorithm to sort the array and calls the CompareAndSwap() function to compare each string and swap them if necessary.

MORE INFORMATION

Sample Code

/*
 * Compile options needed: /MT
 */

#include <afx.h>
#include <iostream.h>
#include <afxcoll.h>

class CSortStringArray : public CStringArray { public:
   void Sort();
private:
   BOOL CompareAndSwap(int pos);
};

void CSortStringArray::Sort()
{
   BOOL bNotDone = TRUE;

   while (bNotDone)
   {
      bNotDone = FALSE;
      for(int pos = 0;pos < GetUpperBound();pos++)
         bNotDone |= CompareAndSwap(pos);
   }
}

BOOL CSortStringArray::CompareAndSwap(int pos) {
   CString temp;
   int posFirst = pos;
   int posNext = pos + 1;

   if (GetAt(posFirst).CompareNoCase(GetAt(posNext)) > 0)
   {
      temp = GetAt(posFirst);
      SetAt(posFirst, GetAt(posNext));
      SetAt(posNext, temp);
      return TRUE;
   }
   return FALSE;
}

void main()
{
   CSortStringArray sortArray;

   sortArray.Add(CString("Zebra"));
   sortArray.Add(CString("Bat"));
   sortArray.Add(CString("Apple"));
   sortArray.Add(CString("Mango"));

   for (int i = 0; i <= sortArray.GetUpperBound(); i++)
      cout << sortArray[i] << endl;

   sortArray.Sort();
   cout << endl;

   for (int j = 0; j <= sortArray.GetUpperBound(); j++)
      cout << sortArray[j] << endl;
}


Additional reference words: kbinf 7.00 1.00 1.50 2.00 2.10 2.50 3.00 4.00
KBCategory: kbprg kbcode
KBSubcategory: MfcMisc
Keywords : MfcMisc kbcode kbprg
Technology : kbMfc
Version : 7.00 | 1.00 1.50 | 1.00 2.00 2
Platform : MS-DOS NT WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 10, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.