Click here to Skip to main content
15,905,686 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 487.1K   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: 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 
I wanted to be able to use a loop to insert the fields of a database into the sortable list control without having to use one function with all the strings passed to it at once.

In the article you noted that you could add things the normal CListCtrl way, but when I did that, the sort just plain bombed. So I modified my insertion list to pass in the string aray that CSortListCtrl creates in AddItem.

I added the following member function:

int CSortListCtrl::AddStrList(const int iIndex, LPTSTR *arrpsz )
{

VERIFY( SetTextArray( iIndex, arrpsz ) );

return iIndex;
}

and the following modification to my code in the dialog that contains the CSortListCtrl:

CString item;

for(h = 0; h < recs; h++)
{
//Insert an item for this:
int iIndex = m_ctlList.InsertItem( cols, "" );
LPTSTR* arrpsz = new LPTSTR[ cols ];

//Loop through to set text for this row:
for(int j = 0; j < cols; j++)
{
set->GetFieldValue(j, item);
m_ctlList.SetItem(h, j, LVIF_TEXT, item, 0, 0, 0, NULL);
arrpsz[ j ] = new TCHAR[ lstrlen( item ) + 1 ];
(void)lstrcpy( arrpsz[ j ], item );
}
m_ctlList.AddStrList( iIndex, arrpsz );

//Move to the next record:
set->MoveNext();
}


Hope that helps people Smile | :)
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 
GeneralRe: Assertion Failure in OnInitDialog Pin
Fred D.16-Feb-02 0:10
Fred D.16-Feb-02 0:10 
GeneralScroll Bars appear disable when we create this control Pin
15-Jan-02 4:39
suss15-Jan-02 4:39 
GeneralGood, but how to draw a rarrow on the header in a SDI AP Pin
16-Dec-01 2:21
suss16-Dec-01 2:21 
GeneralA little bug in my case Pin
Vincent Tan26-Oct-01 0:06
Vincent Tan26-Oct-01 0:06 
QuestionHow do I use this Class in a ViewList? Pin
Lawrence24-Oct-01 5:20
Lawrence24-Oct-01 5:20 
AnswerRe: How do I use this Class in a ViewList? Pin
hongsp23-Dec-01 13:38
hongsp23-Dec-01 13:38 
GeneralItem Data Pin
30-Sep-01 5:54
suss30-Sep-01 5:54 
GeneralA Little BUG Pin
Gilad Novik24-Sep-01 23:13
Gilad Novik24-Sep-01 23:13 
Generalthanks.. Pin
18-Dec-01 17:32
suss18-Dec-01 17:32 
GeneralRe: A Little BUG i added it, but still got bug Pin
kaya1720-Apr-04 18:41
kaya1720-Apr-04 18:41 
GeneralEnhanced CListCtrl that accepts and filters dropped files and folders Pin
9-Sep-01 5:52
suss9-Sep-01 5:52 
QuestionMultiple Column Sort ? Pin
8-Sep-01 9:24
suss8-Sep-01 9:24 
AnswerRe: Multiple Column Sort ? Pin
8-Sep-01 9:48
suss8-Sep-01 9:48 

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.