Zyrenthian
Programmer
I have been working on a CListCtrl for about a day now and I was curious if anyone has ever had the sort algorithm in the list control fail for them.
I have traced through my compare function and the correct values are being returned but the list does not come up sorted!? This is bugging the heck out of me :-(
Here is how I set up the list
( m_ctrlUserNameList is the CListCtrl )
( m_currentSortState is how I will sort the list )
( SORT_LIST_STATES_ENUM are values to determine how to sort)
newIndex = m_ctrlUserNameList.InsertItem(nRow,userName);
m_ctrlUserNameList.SetItem(newIndex,1,LVIF_TEXT,
physName,0,0,0,0);
m_ctrlUserNameList.SetItemData(newIndex,newIndex);
OK, what is happening here is the user name and the physician name are entered into the list into their appropriate column and the set item data sets the values for lParam1 & lParam2 for the compare function
Next I make a call to the sort function (initially, m_currentSortState is equal to ASC_LOGIN_NAME)
m_ctrlUserNameList.SortItems(ListSort,(LPARAM) &m_ctrlUserNameList);
The pointer to m_ctrlUserNameList is used in the compare function.
int CALLBACK CUserNameConfigurationDlg::ListSort(
LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
CListCtrl* pListCtrl = (CListCtrl*)lParamSort;
int returnVal;
CString str1;
CString str2;
switch(m_currentSortState)
{
case ASC_LOGIN_NAME:
str1 = pListCtrl->GetItemText(lParam1,0);
str2 = pListCtrl->GetItemText(lParam2,0);
returnVal = str1.CompareNoCase(str2);
break;
case DEC_LOGIN_NAME:
str1 = pListCtrl->GetItemText(lParam1,0);
str2 = pListCtrl->GetItemText(lParam2,0);
returnVal = str2.CompareNoCase(str1);
break;
case ASC_PHYSICIAN_NAME:
str1 = pListCtrl->GetItemText(lParam1,1);
str2 = pListCtrl->GetItemText(lParam2,1);
returnVal = str1.CompareNoCase(str2);
break;
case DEC_PHYSICIAN_NAME:
str1 = pListCtrl->GetItemText(lParam1,1);
str2 = pListCtrl->GetItemText(lParam2,1);
returnVal = str2.CompareNoCase(str1);
break;
default:
return 0;
}
return returnVal;
}
I dont know why it is not sorting. It has, on a very rare occastion, sorted properly. The properties of my list control are correct as far as I can tell. The strings are coming back properly when i assign str1 and str2 and the value of returnVal is correct from the comparison.
Any ideas, suggestions, etc would be appreciated.
Thanks in advance
Matt
I have traced through my compare function and the correct values are being returned but the list does not come up sorted!? This is bugging the heck out of me :-(
Here is how I set up the list
( m_ctrlUserNameList is the CListCtrl )
( m_currentSortState is how I will sort the list )
( SORT_LIST_STATES_ENUM are values to determine how to sort)
newIndex = m_ctrlUserNameList.InsertItem(nRow,userName);
m_ctrlUserNameList.SetItem(newIndex,1,LVIF_TEXT,
physName,0,0,0,0);
m_ctrlUserNameList.SetItemData(newIndex,newIndex);
OK, what is happening here is the user name and the physician name are entered into the list into their appropriate column and the set item data sets the values for lParam1 & lParam2 for the compare function
Next I make a call to the sort function (initially, m_currentSortState is equal to ASC_LOGIN_NAME)
m_ctrlUserNameList.SortItems(ListSort,(LPARAM) &m_ctrlUserNameList);
The pointer to m_ctrlUserNameList is used in the compare function.
int CALLBACK CUserNameConfigurationDlg::ListSort(
LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
CListCtrl* pListCtrl = (CListCtrl*)lParamSort;
int returnVal;
CString str1;
CString str2;
switch(m_currentSortState)
{
case ASC_LOGIN_NAME:
str1 = pListCtrl->GetItemText(lParam1,0);
str2 = pListCtrl->GetItemText(lParam2,0);
returnVal = str1.CompareNoCase(str2);
break;
case DEC_LOGIN_NAME:
str1 = pListCtrl->GetItemText(lParam1,0);
str2 = pListCtrl->GetItemText(lParam2,0);
returnVal = str2.CompareNoCase(str1);
break;
case ASC_PHYSICIAN_NAME:
str1 = pListCtrl->GetItemText(lParam1,1);
str2 = pListCtrl->GetItemText(lParam2,1);
returnVal = str1.CompareNoCase(str2);
break;
case DEC_PHYSICIAN_NAME:
str1 = pListCtrl->GetItemText(lParam1,1);
str2 = pListCtrl->GetItemText(lParam2,1);
returnVal = str2.CompareNoCase(str1);
break;
default:
return 0;
}
return returnVal;
}
I dont know why it is not sorting. It has, on a very rare occastion, sorted properly. The properties of my list control are correct as far as I can tell. The strings are coming back properly when i assign str1 and str2 and the value of returnVal is correct from the comparison.
Any ideas, suggestions, etc would be appreciated.
Thanks in advance
Matt