Click here to Skip to main content
15,891,248 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 484.9K   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: Missing functions Pin
Daniel Irwin1-Sep-05 2:56
Daniel Irwin1-Sep-05 2:56 
GeneralRe: Missing functions Pin
cristitomi6-Apr-07 4:25
cristitomi6-Apr-07 4:25 
AnswerRe: Missing functions Pin
Coruscant4-Dec-07 11:35
Coruscant4-Dec-07 11:35 
GeneralBug in SetSortArrow Pin
paulg74212-Oct-04 13:49
paulg74212-Oct-04 13:49 
Generalsolution for a problem which is not even a problem to begin with.. Pin
Yawar Maajed11-Jul-04 7:17
Yawar Maajed11-Jul-04 7:17 
GeneralRe: solution for a problem which is not even a problem to begin with.. Pin
AnthonyJ12-Jul-04 2:01
AnthonyJ12-Jul-04 2:01 
GeneralRe: solution for a problem which is not even a problem to begin with.. Pin
Yawar Maajed12-Jul-04 5:50
Yawar Maajed12-Jul-04 5:50 
GeneralRe: solution for a problem which is not even a problem to begin with.. Pin
Chris Hills18-Oct-05 10:58
Chris Hills18-Oct-05 10:58 
Thank you very much for this info on CListCtrl::SortItemsEx() and LVM_SORTITEMSEX, it's speeded up the sorting of a large list by a factor of more than 10 (because I don't have to call CListCtrl::FindItem() all the time).

As you say, CListCtrl::SortItemsEx() doesn't seem to be in MFC6, but neither is LVM_SORTITEMSEX. A google search suggests #define LVM_SORTITEMSEX (LVM_FIRST + 81), which seems to work.

Cheers,
Chris.
GeneralRe: solution for a problem which is not even a problem to begin with.. Pin
sps-itsec469-Mar-06 3:46
sps-itsec469-Mar-06 3:46 
Questionhow to get the current position? Pin
Desvariovsk1-Jun-04 0:15
Desvariovsk1-Jun-04 0:15 
AnswerRe: how to get the current position? Pin
navi200024-Aug-04 2:45
navi200024-Aug-04 2:45 
Generalvery good Pin
Alex Piko13-Apr-04 12:04
Alex Piko13-Apr-04 12:04 
GeneralBug!!!Memory leak!!! Pin
Johnson Chen6-Apr-04 16:55
Johnson Chen6-Apr-04 16:55 
GeneralUnable to create CSortListCtrl object in View Pin
Member 72960631-Dec-03 7:40
Member 72960631-Dec-03 7:40 
QuestionBug!! Can anyone help? Pin
Ahmed Al-Qassem10-Oct-03 11:36
Ahmed Al-Qassem10-Oct-03 11:36 
Questionselect an item/a row? Pin
webwesen6-Oct-03 12:11
webwesen6-Oct-03 12:11 
AnswerRe: select an item/a row? Pin
Member 63412619-Oct-03 19:00
Member 63412619-Oct-03 19:00 
GeneralRe: select an item/a row? Pin
webwesen20-Oct-03 4:32
webwesen20-Oct-03 4:32 
QuestionHow to Get Item Text? Pin
Mikey_E4-Sep-03 21:58
professionalMikey_E4-Sep-03 21:58 
Generalrough code to add arrow to xp-styled ctrls Pin
mightyCoCo20-Aug-03 0:26
mightyCoCo20-Aug-03 0:26 
GeneralRe: rough code to add arrow to xp-styled ctrls Pin
Yin Gang6-Jun-05 23:00
Yin Gang6-Jun-05 23:00 
Generalsome suggestions Pin
sea_soul23-Jul-03 15:39
sea_soul23-Jul-03 15:39 
GeneralError in dynamic creation Pin
Member 37503618-May-03 5:40
Member 37503618-May-03 5:40 
GeneralRe: Error in dynamic creation Pin
fieldart28-Sep-03 16:57
fieldart28-Sep-03 16:57 
GeneralRe: Error in dynamic creation Pin
Manfred Staiger13-Oct-03 3:38
Manfred Staiger13-Oct-03 3:38 

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.