Click to See Complete Forum and Search --> : CListCtrl sorting a column


TraderBoy
September 16th, 1999, 12:36 PM
My CListCtrl in my CFormView is working fine, sorting the items (far left col). I want to be able to click on the 2nd column (1st subitem) heading & sort the list based on the data in that column. Can't find much help in doc. Appreciate any suggs. Thanks in advance to anyone.

Anthony Mai
September 16th, 1999, 01:31 PM
Just handle the LVN_COLUMNCLICK notify message. In the handler, dedermine which column is clicked and that's the column you are going to sort. Then call the SortItems() member function, which requires a pointer to a custom written static function which returns the result of compareing two items.


I hate fat programs as much as I hate being fat myself. I am lean and mean and so is my program.

TraderBoy
September 16th, 1999, 02:14 PM
Thanks for your quick answer. Does the custom written static function have to be global . . or can it be just a member function of the view class? Thanks very much for all your help.

Anthony Mai
September 16th, 1999, 03:07 PM
You need to look at the help for SortItems() member function for CListCtrl.

SortItems is defined as:
BOOL SortItems( PFNLVCOMPARE pfnCompare, DWORD dwData );

The best thing you can do, is write a global function as pfnCompare, and pass this, i.e., pointer to the CListCtrl class itself, as the dwData. This pointer will be passed to your compare function, as well as lParam's of the two items to be compared. You know what to do with them.

The compare function is declared as:
int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);




I hate fat programs as much as I hate being fat myself. I am lean and mean and so is my program.