Sorting list on Numeric Column

bool CMyListCtrl::SortNumericItems( 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;
	int midItem;
	if( hi <= lo ) return FALSE;
	midItem = atoi(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 ) && (atoi(GetItemText(lo, nCol)) < midItem ) )
			while( ( lo < high ) && (atoi(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 ) && (atoi(GetItemText(hi, nCol)) > midItem ) )
			while( ( hi > low ) && (atoi(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(atoi(GetItemText(lo, nCol)) != atoi(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 )
		SortNumericItems( 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 )
		SortNumericItems( nCol, bAscending , lo, high );
	return TRUE;


  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

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

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • In support of their business continuity and disaster recovery plans, many midsized companies endeavor to avoid putting all their eggs in one basket. Understanding the critical role of last-mile connectivity and always available Internet access for their enterprises, savvy firms utilize redundant connections from multiple service providers. Despite the good intentions, their Internet connectivity risk may still be in a single basket. That is because internet service providers (ISPs) and competitive local …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds