Link to home
Start Free TrialLog in
Avatar of Ashurbanipal
Ashurbanipal

asked on

clistctrl sort routine parameters

I created a callback sorting routine for my clistctrl.  The first two arguments are supposed to be the two index numbers of the entries to compare.

static callback int Sort(LPARAM index1, LPARAM index2,

The problem is that index1 and index2 appear to have garbage.  What am I doing wrong?  I used the example straight out of the MSDN documentation.
Avatar of nietod
nietod

Are you sure the 1st two parameters are indexes?  The docs seem to indicate they are pointers to the item data for the items to be compared.
ASKER CERTIFIED SOLUTION
Avatar of endeavor
endeavor

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ashurbanipal

ASKER

Thanks, George.  I can handle it from here.  The MSDN documentation has no relationship to reality.

John
>> The MSDN documentation has no relationship to reality
Why do you say that?  They said the parameters are pointers to the items to be compared.  That, by the way, is what I said in my comment.
You are right, I was wrong.  I skipped over some of the text in the documentation and just cut and pasted the example.  The example is incorrect.

John

CListCtrl::SortItems
BOOL SortItems( PFNLVCOMPARE pfnCompare, DWORD dwData );

Return Value

Nonzero if successful; otherwise zero.

Parameters

pfnCompare

Address of the application-defined comparison function. The comparison function is called during the sort operation each time the relative order of two list items needs to be compared. The comparison function must be either a static member of a class or a stand alone function that is not a member of any class.

dwData

Application-defined value that is passed to the comparison function.

Remarks

Sorts list view items using an application-defined comparison function. The index of each item changes to reflect the new sequence.

The comparison function has the following form:

int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2,
   LPARAM lParamSort);

The comparison function must return a negative value if the first item should precede the second, a positive value if the first item should follow the second, or zero if the two items are equivalent.

The lParam1 and lParam2 parameters specify the item data for the two items being compared. The lParamSort parameter is the same as the dwData value.

Example

// Sort the item in reverse alphabetical order.
static int CALLBACK
MyCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
   // lParamSort contains a pointer to the list view control.
   // The lParam of an item is just its index.
   CListCtrl* pListCtrl = (CListCtrl*) lParamSort;
   CString    strItem1 = pListCtrl->GetItemText(lParam1, 0);
   CString    strItem2 = pListCtrl->GetItemText(lParam2, 0);

   return strcmp(strItem2, strItem1);
}

void snip_CListCtrl_SortItems()
{
   // The pointer to my list view control.
   extern CListCtrl* pmyListCtrl;

   // Sort the list view items using my callback procedure.
   pmyListCtrl->SortItems(MyCompareProc, (LPARAM) pmyListCtrl);
}