Article ID: 167668
Article Last Modified on 11/21/2006
External Name : Sort
Internal Name : Sort
Return Type : long
Parameter List:
Name : vArray
Type : VARIANT*
...
#include <afxpriv.h>
...
long CStrArrayDoc::Sort(VARIANT FAR* vArray)
{
long i, j, min;
BSTR bstrTemp;
SAFEARRAY FAR* psa = NULL;
BSTR HUGEP *pbstr;
HRESULT hr;
DWORD dwTimeStart;
LONG cElements, lLBound, lUBound;
USES_CONVERSION;
// Type check VARIANT parameter. It should contain a BSTR array
// passed by reference. The array must be passed by reference it is
// an in-out-parameter.
if (V_VT(vArray) != (VT_ARRAY | VT_BSTR))
AfxThrowOleDispatchException(1001,
"Type Mismatch in Parameter. Pass a string array by reference");
psa = V_ARRAY(vArray);
// Check dimensions of the array.
if (SafeArrayGetDim(psa) != 1)
AfxThrowOleDispatchException(1002,
"Type Mismatch in Parameter. Pass a one-dimensional array");
dwTimeStart = GetTickCount();
// Get array bounds.
hr = SafeArrayGetLBound(psa, 1, &lLBound);
if (FAILED(hr))
goto error;
hr = SafeArrayGetUBound(psa, 1, &lUBound);
if (FAILED(hr))
goto error;
// Get a pointer to the elements of the array.
hr = SafeArrayAccessData(psa, (void HUGEP* FAR*)&pbstr);
if (FAILED(hr))
goto error;
// Bubble sort.
cElements = lUBound-lLBound+1;
for (i = 0; i < cElements-1; i++)
{
min = i;
for (j = i+1; j < cElements; j++)
{
// NULL is a valid value for a BSTR. This code treats a NULL
// BSTR as less than other string values.
if (pbstr[min] == NULL)
continue;
else if (pbstr[j] == NULL
|| wcscmp(pbstr[j], pbstr[min]) < 0)
min = j;
}
//Swap array[min] and array[i].
bstrTemp = pbstr[min];
pbstr[min] = pbstr[i];
pbstr[i] = bstrTemp;
}
hr = SafeArrayUnaccessData(psa);
if (FAILED(hr))
goto error;
return GetTickCount()-dwTimeStart;
error:
AfxThrowOleDispatchException(1003,
"Unexpected Failure in FastSort method");
return 0;
}
Private Sub Command1_Click()
Dim o As Object
Dim v As Variant
ReDim v(50) As String
Dim SortTime As Long
Set o = CreateObject("StrArray.Document")
upperbound = 1
lowerbound = 100
For n = 0 To 50
v(n) = "Entry " & Int((upperbound-lowerbound+1)*Rnd+lowerbound)
Next n
SortTime = o.Sort(v)
MsgBox ("Done")
End Sub
122287 Limits of Visual Basic 3.0 & Disptest as automation controllers
131046 Binary.exe transfers binary data using OLE automation
131086 Use of safe arrays in automation
140202 MFCArray using safe arrays in MFC automation
122289 Passing structures in OLE automation
154172 How to pass binary data between an ActiveX control and VB
Keywords: kbhowto kbautomation kbfaq KB167668