CListCtrl sorting with sortItem method...

Posted on 1999-09-07
Medium Priority
Last Modified: 2012-05-05
I would like to use the sortItem method provided with the CListCtrl. But when I trace inside my comparision function:
"int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)"

"lParam1" and "lParam2" are null. Maybe the lParamSort should be used ? Can anyone give me a sample of code on how using this function ? Thanks.
Question by:wooper
  • 2
LVL 31

Expert Comment

ID: 2030074
Hi wooper,

The sample code in the MSDN help for CListCtrl::SortItems() should not work. Cause:

The CListCtrl::SortItems() function uses the LVM_SORTITEMS message to sort the list items. Sorting with this message passes the user defined item data (to set with CListCtrl::SetItemData) to the given compare function.
There also exists a LVM_SORTITEMSEX message, which sorts the items by passing the item id's to the given compare function. There is no CListCtrl-wrapper function for this message, but you can use ListView_SortItemsEx() macro to sort the list.

hope that helps,


Author Comment

ID: 2030276
Hmm...  the Visual v4.0 doesn't reconize the "ListView_SortItemsEx()" macro.
Moreover, when I want to use the "ListView_SortItems" macro instead of the "ListView_SortItemsEx()", the compiler sent me a strange error message:

"error C2660: 'SendMessageA' : function does not take 4 parameters"

It's a non sense ?!?

The macro seems to be already there, so...

Here is the code I've used to call it:

HWND a = m_list.GetSafeHwnd();
PFNLVCOMPARE b = CompareFunc;
LPARAM c = dwData;
ListView_SortItems(a, b, c);

What's wrong ?

Accepted Solution

danny_pav earned 200 total points
ID: 2030347
Every time you add an item to the list control, set its item data (CListCtrl::SetItemData(int, DWORD)).  Now when you go to sort the items, lparam1 is the Item Data of item 1 and lParam2 is Item Data of item 2.  

So big deal.  What do I do?

Here's something I do.  When I insert a record, I insert a pointer to an instance of the record.

void CDlgX::InsertRec(const CSomeRec& Rec)
  int nItem = m_ListCtrl.AddItem(....);
  m_ListCtrl.SetItemData(nItem, reinterpret_cast<DWORD>(new CSomeRec(Rec)));

now of course, I have to delete the item when I remove all elements or memory will leak.

void CDlgX::DeleteItem(int nIndex)
DWORD dw = m_ListCtrl.GetItemData(nIndex);
CSomeRec * pRec = reinterpret_cast<CSomeRec *>(dw);
delete pRec;

OK, so I just added some extra stuff but how do I benefit?

In the compare function, the lParams given can be used to determine the data:

int CompareFunc(LPARAM lp1, LPARAM lp2, LPARAM lpSort)
CSomeRec * pRec1 = reinterpret_cast<CSomeRec *>(lp1);
CSomeRec * pRec2 = reinterpret_cast<CSomeRec *>(lp2);

return pRec1->Compare(*pRec2);
LVL 31

Expert Comment

ID: 2030672
Sorry, found out that LVM_SORTITEMSEX requires InternetExplore > 5.0.

The error with the macro seems to be that the macro uses SendMessage instead of ::SendMessage.

So danny_pav's answer is correct.

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

586 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