Click here to Skip to main content
15,881,852 members
Articles / Desktop Programming / MFC
Article

Sort List Control

Rate me:
Please Sign up or sign in to vote.
4.45/5 (64 votes)
6 Sep 20012 min read 483.3K   5.8K   118   139
A list control with inbuilt sorting, and an easier way to add columns and rows

Sample Image - screenshot.jpg

Introduction

This adds sorting to MFC's list control class. Sorting is automatically taken care of: it will sort text, dates and numbers in ascending or descending order, and show an arrow in the appropriate direction in the heading for the sorted column. It also adds some other things that make life easier when using list controls - it is much easier to set the columns and add rows, and it can load and save the column widths.

How to use it

Look at the example to see how it is used. You need to add the files SortListCtrl.cpp/h and SortHeaderCtrl.cpp/h to your project, then associate a CSortListCtrl variable with your list control (you can do this with ClassWizard).

In the OnInitDialog member function of your dialog class you set the columns and their initial widths by calling the list control's SetHeadings function, it takes a string, or a string ID in the string table, which defines the column headings and their widths, 

e.g.

m_ctlList.SetHeadings( _T("Name,120;Date of Birth,90;Score,50") );

Adding rows is very easy, you call the list control's AddItem function with the same number of strings as the number of columns you added, 

e.g.

m_ctlList.AddItem( _T("Mark Jackson"), _T("09/08/1974"), _T("100") );

To remember the columns' widths call the list control's LoadColumnInfo function after setting the headings, add a handler for your dialog's WM_DESTROY message and in there call SaveColumnInfo.

Implementation

It was a REAL pain to do, your callback function gets the item data for the two rows to compare, but what use is that, you need the text to compare! This control stores the text for the columns in the item data, so the compare function can get at it, it also allows users of the control to use the item data as usual.

Credits

The code for drawing the arrow in the header control was written by Zafir Anjum

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Bugempty item interpretation Pin
Member 1391660419-Jul-18 6:04
Member 1391660419-Jul-18 6:04 
QuestionLicense Terms for Sort List Control Pin
Member 46676737-Jan-14 20:10
Member 46676737-Jan-14 20:10 
AnswerRe: License Terms for Sort List Control Pin
RiqiTang4-Nov-14 2:12
RiqiTang4-Nov-14 2:12 
GeneralMy vote of 5 Pin
JunfengGuo3-Oct-13 19:47
JunfengGuo3-Oct-13 19:47 
GeneralMy vote of 4 Pin
jeffywin22-Jun-11 16:53
jeffywin22-Jun-11 16:53 
GeneralLicencing terms and conditions in a commercial product Pin
R. Choudhury27-Mar-11 20:56
R. Choudhury27-Mar-11 20:56 
GeneralRe: Licencing terms and conditions in a commercial product Pin
RiqiTang4-Nov-14 2:13
RiqiTang4-Nov-14 2:13 
GeneralLicensing for commercial application Pin
will626219-Oct-10 5:09
will626219-Oct-10 5:09 
Generaljump to line Pin
addict8527-Jan-09 4:10
addict8527-Jan-09 4:10 
GeneralWasteful approach Pin
Panic2k33-Dec-08 16:28
Panic2k33-Dec-08 16:28 
GeneralWant to add integer Pin
mailtochandra2000@yahoo.com12-Nov-08 0:40
mailtochandra2000@yahoo.com12-Nov-08 0:40 
Questionchange headings? Pin
general_era1-Oct-08 22:21
general_era1-Oct-08 22:21 
AnswerRe: change headings? Pin
EPausU17-Jul-09 0:31
EPausU17-Jul-09 0:31 
GeneralQuestion about License... Pin
BK CHA7-Sep-08 17:05
BK CHA7-Sep-08 17:05 
GeneralNeed help inserting integer items into a list control Pin
dromichetes3-Sep-08 21:33
dromichetes3-Sep-08 21:33 
I work to a program who has a list control and i can't insert integer items, only char. Please tell me if must be used another mask (in VC++ 6) instead of LVIF_TEXT.
QuestionCListCtrl with virtual list and Images? Pin
bosfan13-Aug-08 3:21
bosfan13-Aug-08 3:21 
Questionwhy no selection? Pin
general_era5-May-08 23:13
general_era5-May-08 23:13 
QuestionHow to process List Item click event? Pin
zotiger22228-Nov-07 21:15
zotiger22228-Nov-07 21:15 
GeneralColumn separators are not displaying Pin
a_snm11-Oct-07 2:41
a_snm11-Oct-07 2:41 
QuestionUsing Virtual lists? Pin
bosfan5-Jun-07 22:34
bosfan5-Jun-07 22:34 
GeneralProblem with SetFont() Pin
bosfan18-Apr-07 3:01
bosfan18-Apr-07 3:01 
QuestionChange rowheight in CSortListCtrl!? Pin
bosfan17-Jan-07 2:59
bosfan17-Jan-07 2:59 
Generalderive a CCtrlView from a CSortListCtrl Pin
mimosa30-Apr-06 19:40
mimosa30-Apr-06 19:40 
GeneralHelp problemswhen i open the dialog again Pin
Emiliano907-Feb-06 17:55
Emiliano907-Feb-06 17:55 
GeneralRe: Help problemswhen i open the dialog again Pin
nm_zjf6-Dec-06 15:43
nm_zjf6-Dec-06 15:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.