EarthWeb
Developer.com
Find:
 


SEARCH TECHCRAWLER -----
CodeGuru Links
Visual Basic
Visual C++

Discussion Boards
Book Reviews
Newsletters (subscribe)
Newsletters (archived)
Guru Columns
  • Nothing but .NET
  • Bugs in the Machine
  • The Shell Game
  • Into the IUnknown
  • The Working Class Programming
  • Tempting Templates
  • Programming in the Real World
  • Article Sections
    ATL COM ActiveX
    atl com activex
    shell programming
    C++
    algorithms & formulas
    c++ & mfc
    date & time
    string
    Controls
    button control
    combobox
    edit control
    imagelist control
    listbox control
    listview control
    menu
    other controls
    property sheet
    rich edit control
    static control
    status bar
    toolbar
    treeview control
    Database
    database
    Frameworks
    ui & printing frameworks
    Graphics & Multimedia
    bitmaps & palettes
    directx
    gdi
    multimedia
    opengl
    Internet & Networking
    ie programming
    internet protocols
    isapi
    network protocols
    Visual Studio
    add-ins & macros
    editor tips
    Windows Programming
    ce
    clipboard
    dll
    file & folder
    help systems
    printing
    win32
    system
    Windows & Dialogs
    console
    dialog
    docking window
    doc/view
    splitter
    Interact
    Newsletters
    Guestbook
    Recommend Us!
    About Us




    EarthWeb Sites:
    IT Management
    Networking & Telecommunications
    IT Jobs
    Software Development
    Web Development
    Hardware & Systems
    Community & SIGs
    EarthWeb Worldwide
       

    QSort for CStringArray


    This article was contributed by Chris Losinger.

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

    Date Posted: April 3, 1999

    Comments:

    Add Comment

       

    Give them a visit Click Here!

    Click Here!

    Get the Answers You Need!
    Incentive-based, question & answer Marketplace. The first 5,000 members receive $12 credit! Try it! Knowledge Market.

    For tons of Windows technology resources, check out the Windows Programming directory.

    Target your Technical searches with
    TechCrawler

    Sweepstakes:
    Here's your chance to win Windows 2000 Professional. EarthWeb's sweepstakes runs through July 2. Click here to enter.

    Give them a visit Click Here!

    Click Here!


    ★ wanayoo — archive 1999 http://codeguru.earthweb.com/cpp_mfc/QSort_StringArray.shtmlNouvelle recherche | Portail wanayoo

    Home About Us Subscribe Contact Us
    Use of this site is subject to certain Terms & Conditions.
    Earthweb's statement regarding our users' right to privacy.
    Copyright ©1996-2000 EarthWeb Inc.. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited.