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: (void) bull****ting Pin
Jörgen Sigvardsson9-Dec-04 2:42
Jörgen Sigvardsson9-Dec-04 2:42 
Generalredifing headers Pin
RSE Thomas2-Dec-04 22:43
RSE Thomas2-Dec-04 22:43 
GeneralMissing functions Pin
VKostka15-Nov-04 4:17
VKostka15-Nov-04 4:17 
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 
Hi Mark!
I guess you are not aware of CListCtrl::FindItem function.
in your article you said (you wrote "real" in CAPSEek! | :eek: )
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.


You can always find index of item by providing item data and use FindItem function and once you have index, you can get everything about that item.
Here is what I do inside a list control sorting callback function.
<br />
int <br />
CMyListCtrl::CompareProc(LPARAM lParam1, LPARAM lParam2)<br />
{<br />
  int nRet = 0;<br />
  LVFINDINFO fi = { 0 };<br />
  // set flags to Item Data thingy<br />
  fi.flags = LVFI_PARAM;<br />
  fi.lParam  = lParam1;<br />
  // get the item index<br />
  int item1 = FindItem(&fi);<br />
<br />
  // do the same for other item<br />
  fi.lParam  = lParam2;<br />
  int item2 = FindItem(&fi);<br />
  // get text of the items<br />
  CString csTextItem1 = GetItemText(item1, mnSortCol);<br />
  CString csTextItem2 = GetItemText(item2, mnSortCol);  <br />
  nRet = _tcsicmp((LPCTSTR)csTextItem1, (LPCTSTR)csTextItem2);<br />
  return mbSortAscending ? nRet : -nRet;   <br />
}<br />


Note that mnSortColumn and mbSortAscending are class members, mnSortColumn represents the column on which user is sorting and mbSortAscending represents if we are sorting in ascending or descending order.

There is a drawback to my approach too, item data for every single item need to be unique, if they are not then this function will not sort correctly, I personally never had a situation where I had to provide same item data for multiple items in the list control, but you never know.

Furthermore, if the programmer completely forgets to provide item data for inserted items, this approach will not work at all, so providing unique item data for items is a pre-requisite for this callback to work.

Thanks for sharing your effort with others though, it is appreciable.

/yawar

I have no signature!
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 
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 

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.