RE: Insert Item in Sorted CListCtrl
- From: rodream <rodream@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 15 Apr 2008 21:42:01 -0700
There is no stub in MFC which do binary search.
Binary Search and insert is proper way and I use it in my programs.
Following code is mine which use Data as a CString.
int BinarySearch(CString szSearchData, int nLeft, int nRight, int*
pnRetInsertIndex)
{
int nMiddle;
while( nLeft <= nRight) {
nMiddle = (nLeft + nRight) / 2;
int nCompare = stricmp(szSearchData, m_aryList.GetAt(nMiddle));
if(nCompare == 0) {
return nMiddle;
} else if(nCompare > 0) {
nLeft = nMiddle + 1;
} else {
nRight = nMiddle - 1;
}
}
*pnRetInsertIndex = nLeft;
//printf("Binary Search Return -1 : Left, Right, Middle, %d %d %d\n",
nLeft, nRight, nMiddle);
return -1;
}
--
WebSite :
Realization of Dream { imagine your dream} - http://rodream.net
WebMail :
rodream@xxxxxxxxx
"Nick Meyer" wrote:
Hi,.
I have a CListCtrl in report view which I'm sorting by calling SortItems in
response to the HDN_ITEMCLICK notification. I pass my own comparison
function and a struct that contains the index of the sorted column and a
sorting direction (ascending or descending) to SortItems.
Since MFC has no knowledge of my sorting scheme, what's the easiest / most
efficient way to determine the proper place to insert a new item in the
control when the items are already sorted? Am I stuck doing a binary search
myself, is there some functionality built into MFC?
Thanks,
Nick Meyer
- References:
- Insert Item in Sorted CListCtrl
- From: Nick Meyer
- Insert Item in Sorted CListCtrl
- Prev by Date: RE: CString Issue
- Next by Date: Re: CString Issue
- Previous by thread: Insert Item in Sorted CListCtrl
- Next by thread: 0xC0150010: The activation context being deactivated is not active for the current thread of execution.
- Index(es):
Relevant Pages
|