Click here to Skip to main content
15,894,337 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 485.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

 
GeneralNumber Sort only Works for Integers Pin
8-Jul-02 8:06
suss8-Jul-02 8:06 
QuestionHow to use icons Pin
0-C1n5-Jul-02 13:02
0-C1n5-Jul-02 13:02 
GeneralSort info on update is lost Pin
bishbosh026-Jun-02 5:19
bishbosh026-Jun-02 5:19 
GeneralRe: Sort info on update is lost Pin
bishbosh026-Jun-02 5:31
bishbosh026-Jun-02 5:31 
GeneralWhen I use 'SetItemText', it generates error Pin
4-Jun-02 18:17
suss4-Jun-02 18:17 
GeneralRe: When I use 'SetItemText', it generates error Pin
joEp mEloEn3-Jul-02 4:45
joEp mEloEn3-Jul-02 4:45 
GeneralRe: When I use 'SetItemText', it generates error Pin
mightyCoCo22-Jul-03 6:11
mightyCoCo22-Jul-03 6:11 
QuestionVC5 Version of Sort List Control? Pin
Richard Males4-Jun-02 5:29
Richard Males4-Jun-02 5:29 
I am interested in using this Sort List control with VC5. The description of the control indicates VC4-6, but I have been having problems with running it under VC5.

I have attempted to modify it to VC5, by doing the following:
1) comment out #include <afxdtctl.h> in stdafx.h
2) Use CStringEx instead of CString in SetHeader, in order
to get proper Find() (VC5 Find does not take two parameters)
3) Revise CSortListCtrl::PreSubclassWindow() to get pointer to CHeaderCtrl (no GetHeaderCtrl() in VC5)

When I run under debug, I get an assertion error:
CHeaderCtrl::GetItemCount() line 414 + 46 bytes
?Serialize@CSortHeaderCtrl@@UAEXAAVCArchive@@@Z + 194 bytes
?LoadColumnInfo@CSortListCtrl@@QAEXXZ + 270 bytes
?OnInitDialog@CSortListCtrlTestDlg@@MAEHXZ + 139 bytes

When I run under Release, control seems to work fine. I am using the demo project.

Any help appreciated. Thanks in advance.




Dick Males
Cincinnati, Ohio, USA
QuestionWindows XP Visual Styles? Pin
Alex Cramer3-Jun-02 22:01
Alex Cramer3-Jun-02 22:01 
AnswerRe: Windows XP Visual Styles? Pin
billb211225-Nov-02 11:17
billb211225-Nov-02 11:17 
Generalmemory leaks Pin
9-Apr-02 12:18
suss9-Apr-02 12:18 
GeneralRe: memory leaks Pin
Zizilamoroso24-Apr-02 6:35
Zizilamoroso24-Apr-02 6:35 
GeneralA new way to sort (very easy!!!) Pin
Coolage7-Apr-02 23:55
Coolage7-Apr-02 23:55 
GeneralRe: A new way to sort (very easy!!!) Pin
Morice1-Apr-03 18:30
Morice1-Apr-03 18:30 
GeneralRe: A new way to sort (very easy!!!) Pin
AnthonyJ28-Apr-03 23:48
AnthonyJ28-Apr-03 23:48 
GeneralNot only easy but also very fast Pin
k77716-Jul-11 2:46
k77716-Jul-11 2:46 
QuestionHow do I insert rows with the color I spcify? Pin
3-Apr-02 6:47
suss3-Apr-02 6:47 
AnswerRe: How do I insert rows with the color I spcify? Pin
Ravi Bhavnani3-Apr-02 7:15
professionalRavi Bhavnani3-Apr-02 7:15 
Generaldeleting multiple items ! Pin
2-Apr-02 11:22
suss2-Apr-02 11:22 
GeneralRe: deleting multiple items ! Pin
Tim Smith2-Apr-02 11:43
Tim Smith2-Apr-02 11:43 
GeneralVariable Number of Fields Pin
26-Mar-02 12:51
suss26-Mar-02 12:51 
GeneralRe: Variable Number of Fields Pin
Bob Farrell24-Jan-03 9:58
Bob Farrell24-Jan-03 9:58 
GeneralThis is sweet Pin
Diarrhio1-Feb-02 12:59
Diarrhio1-Feb-02 12:59 
GeneralAssertion Failure in OnInitDialog Pin
Julia Larson26-Jan-02 5:46
Julia Larson26-Jan-02 5:46 
GeneralRe: Assertion Failure in OnInitDialog Pin
Julia Larson26-Jan-02 6:03
Julia Larson26-Jan-02 6:03 

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.