Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 886
  • Last Modified:

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?
  • 2
  • 2
1 Solution
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,

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.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now