Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sorting safearrays

71 views
Skip to first unread message

Paul Felce

unread,
Aug 20, 1999, 3:00:00 AM8/20/99
to
Does anyone know where I can find source code for sorting a safearray,
In particular my array is an array of variable length strings.

I am very new to C++ so the more comments the code has the better.


Yours
Paul Felce

Dan Evens

unread,
Aug 20, 1999, 3:00:00 AM8/20/99
to
Possibly at CodeGuru, though I have not looked.

www.codeguru.com

--
Dan Evens
Standard disclaimers etc. No spam please.


Paul Felce <Pa...@pfelce.freeserve.net> wrote in article
<7piskl$h81$1...@news7.svr.pol.co.uk>...

Marc Bonet

unread,
Aug 20, 1999, 3:00:00 AM8/20/99
to
In article <01beeb19$c36ec980$a4cc0a8a@honzevensd>,
If you want to sort the data inside the array,
you can "download" the data to a less complicated
structure (a bigger enought element array) and
sort it with the old-fashioned, but pretty good,
function quicksort. With quicksort you pass a pointer
to a function that defines your sort criteria, so
you can literally sort anything. After sort your
normal array, just "upload" your data to your
SAFEARRAY.

Best Regards,

Marc Bonet
marc....@eniac.com
--
Is better to know useless things than
don't know anything at all


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.

Payson White

unread,
Aug 20, 1999, 3:00:00 AM8/20/99
to

"Paul Felce" <Pa...@pfelce.freeserve.net> wrote:
>Does anyone know where I can find source code for sorting a safearray,>In particular my array is an array of variable length strings.
>
>I am very new to C++ so the more comments the code has the better.
>
>
>Yours
>Paul Felce
>
>

#include <algorithm>
#include <functional>

// predicate to compare two BSTRs
class BSTRCompare : public std::binary_function<BSTR,BSTR,bool>
{
public:
bool operator()(const BSTR& lhs,const BSTR& rhs) const
{
return CComBSTR(lhs) < CComBSTR(rhs);
}
};

const STRINGARRAY = VT_ARRAY | VT_BSTR;

STMETHODIMP ATLClass::Sort(VARIANT pArray)
{
// make sure pArray contains a string array
if ( (pArray.vt & STRINGARRAY) != STRINGARRAY)
return E_INVALIDARG;

// Get the bounds of the array
long lBound,uBound;
SafeArrayGetLBound( pArray.parray, &lBound);
SafeArrayGetUBound( pArray.parray, &uBound);

// get pointer to data stored in array
BSTR* pbstr;
SafeArrayAccessData( pArray.parray, (void**)&pbstr);

// use standard algorithm to sort array
std::sort( pbstr + lBound, pbstr + uBound, BSTRCompare() );

// release array data pointer
SafeArrayUnaccessData( pArray.parray);

return S_OK;
}

-----------------** -- Posted from CodeGuru -- **-----------------
http://www.codeguru.com/ The website for Visual C++ programmers.


Bill Symmes

unread,
Aug 21, 1999, 3:00:00 AM8/21/99
to
Typically, you'd sort the array of pointers to the strings, rather than the
strings themselves.

Paul Felce <Pa...@pfelce.freeserve.net> wrote in message
news:7piskl$h81$1...@news7.svr.pol.co.uk...

0 new messages