Sorting Error in CListCtrl
-
ClistCtrl has SortItems function that allows us to use an application defined callback function, the problem is that inside that function (MyCompareProc), lParam1 and lParam2 are always same so there is no sorting sice both represent same data. Here is code from MSDN. 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. CListCtrl* pmyListCtrl; // Sort the list view items using my callback procedure. pmyListCtrl->SortItems(MyCompareProc,(LPARAM)myListCtrl);
-
ClistCtrl has SortItems function that allows us to use an application defined callback function, the problem is that inside that function (MyCompareProc), lParam1 and lParam2 are always same so there is no sorting sice both represent same data. Here is code from MSDN. 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. CListCtrl* pmyListCtrl; // Sort the list view items using my callback procedure. pmyListCtrl->SortItems(MyCompareProc,(LPARAM)myListCtrl);
pc_dev wrote:
), lParam1 and lParam2 are always same so there is no sorting sice both represent same data.
The
MSDN
example given is wrong.pc_dev wrote:
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;
Here
lParam1
andlParam2
are theitem data
for these two items. These needs to be set by usingSetItemData
. Refer this[^] article by Ivor S. Sargoytchev.
Prasad MS MVP - VC++
-
pc_dev wrote:
), lParam1 and lParam2 are always same so there is no sorting sice both represent same data.
The
MSDN
example given is wrong.pc_dev wrote:
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;
Here
lParam1
andlParam2
are theitem data
for these two items. These needs to be set by usingSetItemData
. Refer this[^] article by Ivor S. Sargoytchev.
Prasad MS MVP - VC++