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

  • Cloud computing isn't new, but it's still a hot topic in technology circles. Moving to the cloud has done great things for many businesses. When it comes to handling a basic business necessity, cloud computing has brought many improvements to overall business continuity. Using the cloud for DR makes a lot of sense for many, but it's not exactly the most simple concept. Read this eBook to find answers to all the key questions IT professionals have about cloud-based disaster recovery, and helpful info around the …

  • Thanks to wide spread cloud hosting and innovations small businesses can meet and exceed the legacy systems of goliath corporations. Explore the freedom to work how you want, with a phone system that will adapt to your evolving needs and actually save you lots of expense—read Get an Enterprise Phone System without High Cost and Complexity. The article clearly illustrates: The only hardware you'll need is phone equipment for advanced voice and fax. How to join all your employees, mobile devices, …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date