Re: Sorting a CObArray?

From: Adrian Gibbons (adriangibbons_at_yahoo.co.uk)
Date: 03/27/04


Date: 27 Mar 2004 11:07:25 -0800

Thanks for your replies. I've actually found a solution to my problem
from a posting back in 1997:

Google Groups link: http://groups.google.co.uk/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=01bc7518%248dac3770%248adffe26%40tthomaspc

From: H. Tom Thomas (tthomas@seabase.com)
Subject: Re: sorted array
Newsgroups: microsoft.public.vc.mfc
Date: 1997/06/09

(EDITED)

Here is some code that does the trick. Derive a class from CObArray
with a
pure virtual function that you implement based on the attribute you
want to
sort.

// SortObArray.h
class CSortObArray : public CObArray
{
  DECLARE_DYNAMIC(CSortObArray)
public:
  CSortObArray();
  void Sort();
private:
  virtual BOOL CompareAndSwap(int pos) = 0;
};

// SortObArray.cpp
#include "stdafx.h"
#include "SortObArray.h"

IMPLEMENT_DYNAMIC(CSortObArray, CObArray)

CSortObArray::CSortObArray() : CObArray()
{
}

// Sort
void CSortObArray::Sort()
{
  BOOL bNotDone = TRUE;
 
  while (bNotDone)
  {
    bNotDone = FALSE;
    for(int pos = 0;pos < GetUpperBound();pos++)
      bNotDone |= CompareAndSwap(pos);
  }
}

// CompareAndSwap write this according to your needs
BOOL CSortObArray_DerivedClass::CompareAndSwap(int pos)
{
  CObject* temp;
  int posFirst = pos;
  int posNext = pos + 1;

  if(((CSomeObj*)GetAt(posFirst))->m_sKey.CompareNoCase(((CSomeObj*)GetAt(posNext))->m_sKey)
> 0)
  {
    temp = GetAt(posFirst);
    SetAt(posFirst, GetAt(posNext));
    SetAt(posNext, temp);
    return TRUE;
  }

  return FALSE;
}

For my implementation of CompareAndSwap() I've replaced "CSomeObj*"
with "CStringArray*" and "m_sKey" with "GetAt(0)" to compare the first
CString in the CStringArrays. I also moved the "CObject* temp" into
the IF structure and replaced it with "CStringArray* temp =
(CStringArray*)GetAt(posFirst)".

Due to the time pressures on my project and the fact that I do not
know STL already, this is a good enough solution for me. Maybe I'll
have a look at doing it using vectors and the STL in the future.

Hope this is of some help to people of the future :)

Adrian.



Relevant Pages

  • Re: CListCtrl, hide and edit
    ... bool CDialogWithList::CompareAndSwapString1(int pos, bool bAscending) ... CMyObjectInfo *temp; ... bool bNotDone = TRUE; ... So I don't use the sort mechanism in the list control at all. ...
    (microsoft.public.vc.mfc)
  • Re: how can I sum up the values with more than 15 significant digits?
    ... Dim temp As Variant ... Optional Commas As Boolean = True) As String ... Dim Pos As Long ...
    (microsoft.public.excel.misc)
  • Re: Which is best - recordsets/VBA V SQL statements for updating data
    ... If you've analyzed the problem, etc, etc and concluded a temp table is called for, then by all means go for it. ... It has data added to it in the POS screen. ... Once the order has been finished and payment has been received, the data is Inserted from the temp table to the Billing table via an INSERT TO query and then deleted from the temp table. ... The MDB with the temp table in it is compacted when the app is open, each user has their own copy of the temp table MDB in their TEMP folder. ...
    (comp.databases.ms-access)
  • Re: CString; Stripping the filename from the string
    ... // Returns the file portion from a path ... return temp; ... CString GetFolderOnly ... // Strip off the file name so we can direct the file scanning dialog to ...
    (microsoft.public.vc.mfc)
  • Re: [CHALLENGE] finding rightmost zero bit
    ... Bart Vandewoestyne wrote: ... select case (temp) ... pos = bit_size+ 1 ... Regards, ...
    (comp.lang.fortran)