Click here to Skip to main content
15,896,557 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.7K   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

 
GeneralRe: Error in dynamic creation Pin
Manfred Staiger6-Nov-03 4:59
Manfred Staiger6-Nov-03 4:59 
GeneralRe: Error in dynamic creation Pin
t2di4u19-Mar-04 15:13
t2di4u19-Mar-04 15:13 
GeneralRe: Error in dynamic creation Pin
gianct21-Nov-03 0:05
gianct21-Nov-03 0:05 
GeneralRe: Error in dynamic creation Pin
Member 72960630-Dec-03 9:19
Member 72960630-Dec-03 9:19 
GeneralRe: Error in dynamic creation Pin
Adam Bialowas4-Feb-05 6:24
Adam Bialowas4-Feb-05 6:24 
GeneralRe: Error in dynamic creation [modified] Pin
CN_AHONG17-Apr-07 16:40
CN_AHONG17-Apr-07 16:40 
GeneralRe: Error in dynamic creation Pin
xiang_yan20-Aug-10 19:02
xiang_yan20-Aug-10 19:02 
GeneralIsNumber and negative values Pin
Aleksandr Shcherbakov7-May-03 17:54
Aleksandr Shcherbakov7-May-03 17:54 
QuestionHow to sort IP addresses Pin
Corrupt Sektor25-Apr-03 4:01
Corrupt Sektor25-Apr-03 4:01 
AnswerRe: How to sort IP addresses Pin
VS.NOT24-Mar-04 6:25
VS.NOT24-Mar-04 6:25 
GeneralRe: How to sort IP addresses Pin
Jeffrey Walton15-Jun-04 8:18
Jeffrey Walton15-Jun-04 8:18 
GeneralRe: How to sort IP addresses Pin
Jeffrey Walton15-Jun-04 11:45
Jeffrey Walton15-Jun-04 11:45 
GeneralRe: How to sort IP addresses Pin
linfeng121612-May-08 21:34
linfeng121612-May-08 21:34 
Generalgreate code, thanx Pin
isackhizay21-Apr-03 20:34
isackhizay21-Apr-03 20:34 
QuestionSorting, when style set to Icon? Pin
Anonymous17-Apr-03 3:19
Anonymous17-Apr-03 3:19 
GeneralHelp me! Pin
Morice1-Apr-03 14:00
Morice1-Apr-03 14:00 
GeneralRe: Help me! Pin
myoungjin ro2-Apr-03 19:27
myoungjin ro2-Apr-03 19:27 
GeneralRe: Help me! Pin
jack tian28-Jul-03 23:55
jack tian28-Jul-03 23:55 
GeneralTwo fixes Pin
Diarrhio27-Mar-03 9:50
Diarrhio27-Mar-03 9:50 
QuestionHelp! How to used this control in a view? Pin
sway18-Dec-02 19:20
sway18-Dec-02 19:20 
AnswerRe: Help! How to used this control in a view? Pin
Anonymous8-May-03 22:18
Anonymous8-May-03 22:18 
AnswerRe: Help! How to used this control in a view? Pin
Mikey_E31-Aug-03 8:03
professionalMikey_E31-Aug-03 8:03 
GeneralRe: Help! How to used this control in a view? Pin
EIEN27-Sep-03 3:53
EIEN27-Sep-03 3:53 
AnswerRe: Help! How to used this control in a view? Pin
Manfred Staiger13-Oct-03 3:40
Manfred Staiger13-Oct-03 3:40 
AnswerRe: Help! How to used this control in a view? Pin
Member 72960630-Dec-03 9:27
Member 72960630-Dec-03 9:27 

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.