Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

clistctrl sort routine parameters

Posted on 2000-03-18
5
504 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Quicksort a dynamic deque 33 73
multi threaded debug dll option in visual studio 2 119
C Language combined operators 28 114
Safe conversion? 4 72
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

790 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