Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question on CListCtrl::SortItems

Status
Not open for further replies.

Zyrenthian

Programmer
Mar 30, 2001
1,440
US
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

 
Well, I answered my own question. CListCtrl::SetItemData must be called again to renumber the list from top to bottom starting at zero.

m_ctrlUserNameList.SortItems(ListSort,(LPARAM)
&m_ctrlUserNameList);

for(int i = 0;i<m_ctrlUserNameList.GetItemCount();i++)
m_ctrlUserNameList.SetItemData(i,i);



I have not looked into the reason for this fully but I think the data gets swapped but the ItemData does not.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top