Problem with Sorting in CListCtrl

I am new to the VC++ environment. I am trying to build a small application which uses the CListCtrl class. Everything seems to be working fine. But am faced with a slight problem while using the SortItems method. i have 6 colums - data - id - email - data - name - filename
i have used the "OnColumnclick......" command to  sort my list when the tab in each column is clicked based on the column index. The code is as follows:
void CArchivedView::OnColumnclickInfolist(NMHDR* pNMHDR, LRESULT* pResult)
     int ret ;
            case 0:
                    m_infolist.SortItems(&CVSortByDT, (LPARAM)&m_infolist) ;
                    break ;

          case 5:
                    m_infolist.SortItems(&CVSortByFileName, (LPARAM)&m_infolist) ;
                                  break ;

     *pResult = 0;
m_infolist is the member of the CArchivedview class.

by callback function for say the case 5 is:
int CALLBACK CVSortByFileName(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
     CListCtrl* pListCtrl = (CListCtrl*) lParamSort;
     CString str1 = pListCtrl->GetItemText(lParam1,5) ;
     AfxMessageBox(str1) ;
     CString str2 = pListCtrl->GetItemText(lParam2,5) ;
     AfxMessageBox(str2) ;

     int ret = str1.CompareNoCase(str2) ;
     return ret ;

this is where the problem is - this function doenst seem to work. Am using the message box to print the string of each parameter. It always prints the first entry in the column alone(all the time). I'm not sure whats wrong. I tokk this piece of code from the MSDN as is. Can anyone help with this?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi ajaysusarla,

the problem here is that CListCtrl::SortItems() sends the
LVM_SORTITEMS to the message box. The message handler for
this message uses the passed callback function to sort.
The 'lParam's passed to the function are the user-defined
data associated with the item (with CListCtrl::SetItemData()),
not the item's indicies.

Now you have two possibilities:
1. you find a way to sort the list only through access to the
user-defined data.

2. you use the LVM_SORTITEMSEX message which does exactly what
you want ... it passes the HWND of the list-control and the indicies of the two items to compare.
Problem with LVM_SORTITEMSEX is that it is only available
with 'Windows 2000, Windows NT 4.0 with Internet Explorer 5, Windows 98'

In both cases I would suggest that you pass the clicked column
ID as lParamSort instead of a pointer to the list.
So you can have one single callback function which knows
by this parameter which column is used to sort.

I would strictly suggest not to pass the list control pointer
to the sort function in case of LVM_SORTITEMS and to iterate
through the lists items to find the item's indicies from its
user-defined data ... this may decrease performance very much.

hope that helps,


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ajaysusarlaAuthor Commented:
Hey thanx Zoppo,
That Worked! Thanx a million (again).

Your welcome,

have a nice day,


ajaysusarlaAuthor Commented:
I followed Zoppos instructions with a little bit of improvisation to suit my app. And it worked.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.