CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443

    MFC STL: How can I sort a 'CArray' (or 'CStringArray', 'CIntArray', etc.)?

    Q: How can I sort a 'CArray' (or 'CStringArray', 'CIntArray', etc.)?

    A: If the CxxxArray's items can be compared, then use 'std::sort':

    Ascending:

    Code:
    #include <algorithm>
    
    CArray<int, int&> MyCArray;
    CStringArray MyStringArray;
    
    // Sort the CArray of ints
    std::sort(MyCArray.GetData(), MyCArray.GetData() + MyCArray.GetSize());
    
    // Sort the CStringArray
    std::sort(MyStringArray.GetData(), MyStringArray.GetData() + MyStringArray.GetSize());
    Descending:

    You need to supply a comparison function.

    Code:
    #include <algorithm>
    
    bool SortDescendingInt(const int& x, const int& y)
    {
       return x > y;
    }
    
    bool SortDescendingString(const CString& s1, 
                              const CString& s2)
    {
       return s1 > s2;
    }
    
    CArray<int, int> MyCArray;
    CStringArray MyStringArray;
    
    // Sort the CArray of ints
    std::sort(MyCArray.GetData(), 
              MyCArray.GetData() + MyCArray.GetSize(), 
              SortDescendingInt);
    
    // Sort the CStringArray
    std::sort(MyStringArray.GetData(), 
              MyStringArray.GetData() + MyStringArray.GetSize(),
              SortDescendingString);
    FAQ contributed by: [Paul McKenzie]


    Last edited by Andreas Masur; July 24th, 2005 at 03:30 PM.

  2. #2
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: MFC STL: How can I sort a 'CArray' (or 'CStringArray', 'CIntArray', etc.)?

    One alternative solution is to use the ANSI C function qsort

    Next example sorts ascending, non case-sensitive the elements of a CStringArray.
    Code:
       CStringArray arr;
    
       arr.Add(_T("barbu")); // just for example
       arr.Add(_T("ANNA"));
       arr.Add(_T("ZOE"));
       arr.Add(_T("BUBU"));
       arr.Add(_T("bob"));
       arr.Add(_T("MIKI"));
       // ...
    
       qsort(arr.GetData(), arr.GetSize(), sizeof(CString*), CompareAscNoCase);
    Code:
    int CompareAscNoCase(const void* left, const void* right)
    {
        return ((CString*)left)->CompareNoCase(*((CString*)right));
    }
    Last edited by ovidiucucu; July 3rd, 2008 at 07:42 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  3. #3
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: MFC STL: How can I sort a 'CArray' (or 'CStringArray', 'CIntArray', etc.)?

    Beginning with Visual Studio 2010, can also handily use lambda expressions like in the following example:

    Code:
        // ...
     
        bool bAscending = true;
        // ...
     
        std::sort(arr.GetData(), arr.GetData() + arr.GetSize(),                           
            [bAscending]                                             // lambda introducer
            (const CString& left, const CString& right)              // lambda formal parameters list
            { return bAscending ? (left < right) : (left > right); } // lambda body
            );
        // ...


    See also:
    Last edited by ovidiucucu; January 30th, 2015 at 10:37 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured