Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

clistctrl sort routine parameters

Posted on 2000-03-18
5
Medium Priority
?
515 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
[X]
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
  • 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 200 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

610 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