hi,
I'm developing an MFC dialog based application.
in my dialog i have a CListCtrl object in which i've inserted 3 columns. Whenever I insert a new item in the list i set it's itemdata equal to it's index.
what i'm trying to do is sort the items in alphabetical order based on the text in a selected column.
so when the user clicks on the column the items should be sorted.
I trapped the LVN_COLUMNCLICK notification and in the hadling function i put the following:
void CMyDlg::OnColumnclickList(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
nSortedCol = pNMListView->iSubItem;
//nSortedCol is a static member variable of the class CMyDlg
m_list.SortItems(SortList, (LPARAM) &m_list);
*pResult = 0;
}
I defined the compare function as a static member as follows:
int CEmailNotDlg::SortList(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
CListCtrl* pListCtrl = (CListCtrl*) lParamSort;
CString strItem1 = pListCtrl->GetItemText(lParam1, nSortedCol);
CString strItem2 = pListCtrl->GetItemText(lParam2, nSortedCol);
int ret=-wcsicmp(strItem2, strItem1);
return ret;
}
Now here comes the question:
when I first click on a colum the sorting is carried out perfectly, HOWEVER if I click again the rows are switched randomly. What's going on??
i would really appreciate any sort of help
thx a lot
I'm developing an MFC dialog based application.
in my dialog i have a CListCtrl object in which i've inserted 3 columns. Whenever I insert a new item in the list i set it's itemdata equal to it's index.
what i'm trying to do is sort the items in alphabetical order based on the text in a selected column.
so when the user clicks on the column the items should be sorted.
I trapped the LVN_COLUMNCLICK notification and in the hadling function i put the following:
void CMyDlg::OnColumnclickList(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
nSortedCol = pNMListView->iSubItem;
//nSortedCol is a static member variable of the class CMyDlg
m_list.SortItems(SortList, (LPARAM) &m_list);
*pResult = 0;
}
I defined the compare function as a static member as follows:
int CEmailNotDlg::SortList(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
CListCtrl* pListCtrl = (CListCtrl*) lParamSort;
CString strItem1 = pListCtrl->GetItemText(lParam1, nSortedCol);
CString strItem2 = pListCtrl->GetItemText(lParam2, nSortedCol);
int ret=-wcsicmp(strItem2, strItem1);
return ret;
}
Now here comes the question:
when I first click on a colum the sorting is carried out perfectly, HOWEVER if I click again the rows are switched randomly. What's going on??
i would really appreciate any sort of help
thx a lot