Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


CListCtrl sort

Posted on 1999-06-28
Medium Priority
Last Modified: 2013-11-20
Hi, i have a problem with sorting a CListCtrl control which has 3 columns. I have written a callback function that I call with the SortItems function of the CListCtrl, but it doesn't work properly.  Here's the call and the sort function :

// I initialize and add some data in the CListCtrl
int CALLBACK CompareItems(LPARAM   lparam1, LPARAM lparam2,              LPARAM lparamsort)
  CListCtrl* pList = (CListCtrl*) lparamsort;
  CString strItem1 = pList->GetItemText(lparam1,0);
  CString strItem2 = pList->GetItemText(lparam2,0);
  return strcmp(strItem1,strItem2);

Where m_list_codes is the CListCtrl I created with the ressource editor. When I debug, what is wrong is that the function is called with lparam1 and lparam2 that have values like 22932876 or something like that when in fact, they should have the index of a data in the CListCtrl. Is there some conversion I forgot ? Or is it my sort function that is not correct ?  Thanks for your help.
Question by:progbie
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

Accepted Solution

SD_Adept earned 100 total points
ID: 1308185

look at the parameter list for the compare items.  lparam and wparam actually are the following

afx_msg int OnCompareItem( int nIDCtl, LPCOMPAREITEMSTRUCT lpCompareItemStruct );

what you want to do, is cast your lparam2 to an LPCOMPAREITEMSSTRUCT like this


then use that struct to determine the return value

here is a copy of the compare items structure

typedef struct tagCOMPAREITEMSTRUCT {
    UINT   CtlType;
    UINT   CtlID;
    HWND   hwndItem;
    UINT   itemID1;
    DWORD  itemData1;
    UINT   itemID2;
    DWORD  itemData2;

based on that data, you need to return a value from that routine for whether the items are equal to, greater than or less than.

–1 Item 1 sorts before item 2.
0 Item 1 and item 2 sort the same.
1 Item 1 sorts after item 2.


Author Comment

ID: 1308186
I don't really understand why I have to do that, and where the compareitemstruct comes from.  According to msdn documentation on CListCtrl::SortItems, all I'm supposed to have to do is write a compare function (here, I call it CompareItems) that will be called during the sort operation each time the relative order of two list items needs to be compared.  Also, lparam1 and lparam2 are supposed to contain the index of the items to be compared.  What I don't understand is why it is not what it contains.

Featured Post

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…

670 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