CListCtrl.. sort on column 2

Posted on 2000-02-04
Medium Priority
Last Modified: 2013-11-20
I have a listctrl in a dialog box. I want to sort if the user clicks on the column. So i mapped the message "LVN_COLUMNCLICK" and call the sort function(which is static member function) and pass pointer to listctrl as the paramter.. as
m_lstFiles.SortItems( SortFunc, &m_lstFiles);

In the sort function how do I get the data in the second column. I tried using GetItemText but returns the text of first column always.? The two columns are FileName and FileSize. I want to sort on file size?
Please write the entire function for me to cut and paste. I have given good points and will consider increasing it..
Question by:jayku
  • 2
  • 2
LVL 23

Accepted Solution

chensu earned 4400 total points
ID: 2492096
Sorting the list based on text in any column

Sort list based on text/numeric/date-time in any column

Sorting list on Numeric Column
LVL 23

Expert Comment

ID: 2492101
Sorting list based on integers in any column

Sorting the list when user clicks on column header

Expert Comment

ID: 2492106
Greetings Jayku,
Here's a description that should answer your problems.

Sorting the list when user clicks on column header

If you don’t want to allow the users to sort the list by clicking on the header, you can use the style LVS_NOSORTHEADER. However, if you do want to allow sorting, you do not specify the LVS_NOSORTHEADER. The control, though, does not sort the items. You have to handle the HDN_ITEMCLICK notification from the header control and process it appropriately. You may choose to sort the items in a different manner.

Step 1: Add two member variables to the CListCtrl. The first variable to track which column has been sorted on, if any. The second variable to track if the sort is ascending or descending.
        int nSortedCol;
        BOOL bSortAscending;

Step 2: Initialize them in the constructor.
Initialize nSortedCol to -1 to indicate that no column has been sorted on. If the list is initially sorted, then this variable should reflect that.
        nSortedCol = -1;
        bSortAscending = TRUE;
Step 3: Add entry in message map to handle HDN_ITEMCLICK
Actually you need to add two entries. For HDN_ITEMCLICKA and HDN_ITEMCLICKW. Do not use the class wizard to add the entry. For one, you need to add two entries whereas the class wizard will allow you only one. Secondly, the class wizard uses the wrong macro in the entry. It uses ON_NOTIFY_REFLECT() instead of ON_NOTIFY(). Since the HDN_ITEMCLICK is a notification from the header control to the list view control, it is a direct notification and not a reflected one.
 Note that we specify the same function for both the notification. Actually the program will receive one or the other and not both. What notification it receives will depend on the OS. The list view control on Windows 95 will send the ANSI version and the control on NT will send the UNICODE version.
Also, note that the second argument is zero. This value filters for the id of the control and we know that header control id is zero.

Step 4: Write the OnHeaderClicked() function
Here’s where you decide what to do when the user clicks on a column header. The expected behaviour is to sort the list based on the values of the items in that column. Use the Sort() function that you have developed in place of the one I have put in there. If any of the columns displays numeric or date values, then you would have to provide custom sorting for them.
void CMyListCtrl::OnHeaderClicked(NMHDR* pNMHDR, LRESULT* pResult)
        HD_NOTIFY *phdn = (HD_NOTIFY *) pNMHDR;

        if( phdn->iButton == 0 )
                // User clicked on header using left mouse button
                if( phdn->iItem == nSortedCol )
                        bSortAscending = !bSortAscending;
                        bSortAscending = TRUE;

                nSortedCol = phdn->iItem;
                SortItems( nSortedCol, bSortAscending );

        *pResult = 0;

Author Comment

ID: 2498562
Adjusted points to 1100

Author Comment

ID: 2498563
Yeah! this cleared all my questions and I was able to implement what I wanted.

Execellent help..100 bonus points..enjoy



Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
The video provides a quick and easy steps to migrate MBOX file to well known Outlook PST and Office 365. Besides this, it also supports and migrates more than 20 email clients of MBOX which include AppleMail, Opera, Thunderbird and SeaMonkey effortl…
Suggested Courses

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question