Solved

clistctrl sort routine parameters

Posted on 2000-03-18
5
494 Views
Last Modified: 2012-06-27
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.
0
Comment
Question by:Ashurbanipal
  • 2
  • 2
5 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 2632511
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.
0
 

Accepted Solution

by:
endeavor earned 50 total points
ID: 2633520
Sort returns the item data parameter of the list item, not the item index. See the docs for LV_ITEM struct.  You need to make sure you set the item data by using the SetItemData method, ListView_SetItemMethod macro, SendMessage LVM_SETITEMDATA directly.  Use SortItemEx if you want the item index. I don't use MFC so I am guessing at the calls method call.

the sort callback doesn't seem to have been thought through very well my MS - and seems hacky to me.  I think they figured you would use the lParams as indexes into a hash/map table or something. Anyway, it works.  i can send you sample code offline if you want (email me at george@passedge.com).

George
0
 

Author Comment

by:Ashurbanipal
ID: 2634288
Thanks, George.  I can handle it from here.  The MSDN documentation has no relationship to reality.

John
0
 
LVL 22

Expert Comment

by:nietod
ID: 2634474
>> 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.
0
 

Author Comment

by:Ashurbanipal
ID: 2637871
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);
}

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now