How to sort the items of a CListCtrl or CListView(CReportView)?

First, when you add items, you need to call SetItemData to attach an data structure(an integer or a pointer) to the item.

Then, define a callback sort function.

int CALLBACK SortFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);

And here, the prarameters lParam1 and lParam2 are the data structure you set in SetItemData. With the parameters, you can get the item content and calculate the sort order. Please notice, the parameters are NOT the item index.

Last, you can reponse the message HDN_ITEMCLICK or LVN_COLUMNCLICK, and call CListCtrl::SortItems with the callback function.

The examples from the CListCtrl::SortItems in MSDN is obsolescent, you’d better refer to the KB article:

250614 HOWTO: Sort Items in a CListCtrl in Report View