Sorting the list based on text in any column

The list view control has two directly supported mechanism for sorting the items in the list. One of them is to use the styles - LVS_SORTASCENDING or LVS_SORTDESCENDING when creating the control. Any item inserted into the list is then automatically inserted in a sorted order. Although this does not involve any extra programming work, it does have the drawbacks that the sorting is always based on the item text and cannot be sorted on sub items and also that any later changes or edits to the item does not update the sorting order.

The second supported mechanism for sorting a list is by using the SortItems() function. This method is very versatile but requires that the program keep track of the items in the list using the item data. Therefore, before the SortItems() function can be used, each item in the list should have an item data associated with it. You normally associate data with an item by using InsertItem() or SetItemData(). SortItems() also requires the address of an application-defined comparison function. The comparison function is called during the sort operation each time the relative order of two list items needs to be compared. The comparison function must be either a static member of a class or a stand alone function that is not a member of any class. The comparision function is called with the item data of the items being compared as arguments to the function. One common mistake is to assume that the arguments passed to the comparision function are the row indices of the items.

The code given below takes a custom approach to sorting and can be reused easily. The SortTextItems() can sort the list based on the text in any of the columns. A shortcoming of this function is that if the column represents a numeric value the sort order is probably not the one you want. SortTextItems() is a recursive function and uses the quick sort algorithm. The last two arguments are mostly for recursive calls and the function declaration in the class should have default values of zero and one assigned to them. When swapping two rows, each subitem, image and state information also has to be swapped.

// SortTextItems	- Sort the list based on column text
// Returns		- Returns true for success
// nCol			- column that contains the text to be sorted
// bAscending		- indicate sort order
// low			- row to start scanning from - default row is 0
// high			- row to end scan. -1 indicates last row
BOOL CMyListCtrl::SortTextItems( int nCol, BOOL bAscending, 
					int low /*= 0*, int high /*= -1* )
	if( nCol >= ((CHeaderCtrl*)GetDlgItem(0))->GetItemCount() )
		return FALSE;

	if( high == -1 ) high = GetItemCount() - 1;

	int lo = low;
	int hi = high;
	CString midItem;

	if( hi <= lo ) return FALSE;

	midItem = GetItemText( (lo+hi)/2, nCol );

	// loop through the list until indices cross
	while( lo <= hi )
		// rowText will hold all column text for one row
		CStringArray rowText;

		// find the first element that is greater than or equal to 
		// the partition element starting from the left Index.
		if( bAscending )
			while( ( lo < high ) && ( GetItemText(lo, nCol) < midItem ) )
			while( ( lo < high ) && ( GetItemText(lo, nCol) > midItem ) )

		// find an element that is smaller than or equal to 
		// the partition element starting from the right Index.
		if( bAscending )
			while( ( hi > low ) && ( GetItemText(hi, nCol) > midItem ) )
			while( ( hi > low ) && ( GetItemText(hi, nCol) < midItem ) )

		// if the indexes have not crossed, swap
		// and if the items are not equal
		if( lo <= hi )
			// swap only if the items are not equal
			if( GetItemText(lo, nCol) != GetItemText(hi, nCol))
				// swap the rows
				LV_ITEM lvitemlo, lvitemhi;
				int nColCount = 
				rowText.SetSize( nColCount );
				int i;
				for( i=0; i<nColCount; i++)
					rowText[i] = GetItemText(lo, i);
				lvitemlo.mask = LVIF_IMAGE | LVIF_PARAM | LVIF_STATE;
				lvitemlo.iItem = lo;
				lvitemlo.iSubItem = 0;
				lvitemlo.stateMask = LVIS_CUT | LVIS_DROPHILITED | 

				lvitemhi = lvitemlo;
				lvitemhi.iItem = hi;

				GetItem( &lvitemlo );
				GetItem( &lvitemhi );

				for( i=0; i<nColCount; i++)
					SetItemText(lo, i, GetItemText(hi, i));

				lvitemhi.iItem = lo;
				SetItem( &lvitemhi );

				for( i=0; i<nColCount; i++)
					SetItemText(hi, i, rowText[i]);

				lvitemlo.iItem = hi;
				SetItem( &lvitemlo );


	// If the right index has not reached the left side of array
	// must now sort the left partition.
	if( low < hi )
		SortTextItems( nCol, bAscending , low, hi);

	// If the left index has not reached the right side of array
	// must now sort the right partition.
	if( lo < high )
		SortTextItems( nCol, bAscending , lo, high );

	return TRUE;


Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: September 10, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild". This loop of continuous delivery and continuous feedback is …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds