Click here to Skip to main content
Click here to Skip to main content

Sort List Control

, 6 Sep 2001
Rate this:
Please Sign up or sign in to vote.
A list control with inbuilt sorting, and an easier way to add columns and rows
<!-- Download Links --> <!-- Article image -->

Sample Image - screenshot.jpg

<!-- Main HTML starts here -->

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

Share

About the Author

Mark Jackson
Web Developer
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
QuestionLicense Terms for Sort List Control PinmemberMember 46676737-Jan-14 21:10 
AnswerRe: License Terms for Sort List Control PinmemberRiqiTang4-Nov-14 3:12 
GeneralMy vote of 5 PinmemberJunfengGuo3-Oct-13 20:47 
GeneralMy vote of 4 Pingroupjeffywin22-Jun-11 17:53 
GeneralLicencing terms and conditions in a commercial product PinmemberR. Choudhury27-Mar-11 21:56 
GeneralRe: Licencing terms and conditions in a commercial product PinmemberRiqiTang4-Nov-14 3:13 
GeneralLicensing for commercial application Pinmemberwill626219-Oct-10 6:09 
Generaljump to line Pinmemberaddict8527-Jan-09 5:10 
GeneralWasteful approach PinmemberPanic2k33-Dec-08 17:28 
GeneralWant to add integer Pinmembermailtochandra2000@yahoo.com12-Nov-08 1:40 
Questionchange headings? Pinmembergeneral_era1-Oct-08 23:21 
AnswerRe: change headings? PinmemberEPausU17-Jul-09 1:31 
GeneralQuestion about License... PinmemberBK CHA7-Sep-08 18:05 
GeneralNeed help inserting integer items into a list control Pinmemberdromichetes3-Sep-08 22:33 
Questionwhy no selection? Pinmembergeneral_era6-May-08 0:13 
QuestionHow to process List Item click event? Pinmemberzotiger22228-Nov-07 22:15 
GeneralColumn separators are not displaying Pinmembera_snm11-Oct-07 3:41 
QuestionUsing Virtual lists? Pinmembertermal5-Jun-07 23:34 
GeneralProblem with SetFont() Pinmembertermal18-Apr-07 4:01 
QuestionChange rowheight in CSortListCtrl!? Pinmemberbreak;17-Jan-07 3:59 
Generalderive a CCtrlView from a CSortListCtrl Pinmembermimosa30-Apr-06 20:40 
GeneralHelp problemswhen i open the dialog again PinmemberEmiliano907-Feb-06 18:55 
GeneralRe: Help problemswhen i open the dialog again Pinmembernm_zjf6-Dec-06 16:43 
GeneralSelecting a row PinmemberZoltan4-Jan-06 5:33 
GeneralRe: Selecting a row Pinmemberfrankpetersen19-May-07 8:21 
QuestionHelp the control causes a crash Pinmembertarink28-Nov-05 13:43 
GeneralNeed help to use the Sort Iist Control with XP-Style! Pinmembermirso6730-May-05 23:41 
GeneralRe: Need help to use the Sort Iist Control with XP-Style! PinmembereXRange22-Jun-05 17:21 
GeneralRe: Need help to use the Sort Iist Control with XP-Style! Pinmembermavru20-Nov-08 5:11 
GeneralRe: Need help to use the Sort Iist Control with XP-Style! Pinmembertermal20-Nov-08 6:44 
QuestionUNICODE? Pinmembertrandainghia23-May-05 21:59 
AnswerRe: UNICODE? Pinmemberas_sound_as30-Jan-07 4:22 
Generalslight tightening of compare function selection Pinmemberdbarndt25-Apr-05 5:13 
GeneralRe: slight tightening of compare function selection PinmemberRedFraggle5-Mar-06 6:13 
QuestionRe: slight tightening of compare function selection PinmemberRui Frazao8-May-07 7:10 
General(void) bullshitting PinmemberIgnorant Stroustrup9-Dec-04 2:29 
GeneralRe: (void) bull****ting PinmemberJörgen Sigvardsson9-Dec-04 3:42 
Generalredifing headers PinmemberRSE Thomas2-Dec-04 23:43 
GeneralMissing functions PinmemberVKostka15-Nov-04 5:17 
GeneralRe: Missing functions PinmemberDaniel Irwin1-Sep-05 3:56 
GeneralRe: Missing functions Pinmembercristitomi6-Apr-07 5:25 
AnswerRe: Missing functions PinmemberCoruscant4-Dec-07 12:35 
GeneralBug in SetSortArrow Pinmemberpaulg74212-Oct-04 14:49 
Generalsolution for a problem which is not even a problem to begin with.. PinmemberYawar Maajed11-Jul-04 8:17 
GeneralRe: solution for a problem which is not even a problem to begin with.. PinmemberAnthonyJ12-Jul-04 3:01 
GeneralRe: solution for a problem which is not even a problem to begin with.. PinmemberYawar Maajed12-Jul-04 6:50 
GeneralRe: solution for a problem which is not even a problem to begin with.. PinmemberChris Hills18-Oct-05 11:58 
GeneralRe: solution for a problem which is not even a problem to begin with.. Pinmembermykel9-Mar-06 4:46 
Questionhow to get the current position? PinmemberDesvariovsk1-Jun-04 1:15 
AnswerRe: how to get the current position? Pinmembernavi200024-Aug-04 3:45 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150414.1 | Last Updated 7 Sep 2001
Article Copyright 2001 by Mark Jackson
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid