Determining row indices in Sorting Comparison function
Posted
by Kamalahasan Rajaram
on August 6th, 1998
/////////////////////////////////////////////////////////////////////////////
// SortInfo structure definition
typedef struct tagSortInfo
{
CListCtrl * pListControl;
int nColumnNo;
bool nAscendingSortOrder;
}SortInfo;
/////////////////////////////////////////////////////////////////////////////
// CTestDialog dialog
class CTestDialog : public CDialog
{
public:
CTestDialog(CWnd* pParent = NULL); // standard constructor
//Dialog Data
//{{AFX_DATA(CTestDialog)
enum { IDD = IDD_DIALOG1 };
CListCtrl m_ListControl;
//}}AFX_DATA
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CTestDialog)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
static int CALLBACK SortList(LPARAM lpOne, LPARAM lpTwo, LPARAM lpArg);
// Implementation
SortInfo m_SortInfo;
protected:
// Generated message map functions
//{{AFX_MSG(CTestDialog)
//}}AFX_MSG
afx_msg void OnHeaderClicked(NMHDR* pNMHDR, LRESULT* pResult);
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//
void CTestDialog::OnHeaderClicked(NMHDR* pNMHDR, LRESULT* pResult)
{
HD_NOTIFY *phdn = (HD_NOTIFY *) pNMHDR;
if(phdn->iButton == 0)
{
if (phdn->iItem == m_SortInfo.nColumnNo)
m_SortInfo.nAscendingSortOrder = !m_SortInfo.nAscendingSortOrder;
else
m_SortInfo.nAscendingSortOrder = TRUE;
m_SortInfo.nColumnNo = phdn->iItem;
m_SortInfo.pListControl = &m_ListControl;
m_ListControl.SortItems( SortList,(DWORD)&m_SortInfo );
}
*pResult = 0;
}
int CALLBACK CTestDialog::SortList(LPARAM lpOne, LPARAM lpTwo, LPARAM lpArg)
{
int nResult = 0;
SortInfo * pSortInfo = (SortInfo *) lpArg;
int nColumnNo = pSortInfo->nColumnNo;
CListCtrl * pListControl = pSortInfo->pListControl;
bool nAscendingSortOrder = pSortInfo->nAscendingSortOrder;
int lFirstData = -1, lSecondData = -1;
LV_FINDINFO FindInfo;
// use LVFI_WRAP for cases where lpTwo represents a row before lpOne
FindInfo.flags = LVFI_PARAM | LVFI_WRAP;
FindInfo.lParam = lpOne;
lFirstData = pListControl->FindItem(&FindInfo);
FindInfo.lParam = lpTwo;
// reduce searching time by setting the start row as lFirstData
lSecondData = pListControl->FindItem(&FindInfo,lFirstData);
// because we are searching for LPARAM sent to us, FindItem() on
// these values should always be successful
ASSERT(lFirstData != -1); ASSERT(lSecondData != -1);
CString FirstText = pListControl->GetItemText(lFirstData,nColumnNo);
CString SecondText = pListControl->GetItemText(lSecondData,nColumnNo);
int nCompareValue = FirstText.Compare(SecondText);
nResult = nCompareValue * ((nAscendingSortOrder)?1:-1);
// or
// your own comparison code
return nResult;
}
Last updated: 5 August 1998

Comments
For Beginners as me - Important!
Posted by elk11 on 02/16/2006 09:30amThanks
Posted by Legacy on 11/24/2003 12:00amOriginally posted by: Aaron J
Excellent article. The procedure to find the items was just what I was looking for.
Reply
Nice Job ! Thank You !
Posted by Legacy on 03/28/2003 12:00amOriginally posted by: Jack Porter
Clearer and more thorough than the MSDN example.
ReplyImportant!
Posted by Legacy on 02/18/2002 12:00amOriginally posted by: Stefan
ReplyMicrosoft needs your help
Posted by Legacy on 01/12/2002 12:00amOriginally posted by: Nat Pennathur
I struggled with code samples I got from Microsoft documentation, that had a wierd sorting algorithm happening. Only MS could have conceived one. Copied your code as is, and worked like a charm. Thanks a mil !!!
ReplySORT WAS NOT DONE.
Posted by Legacy on 12/21/2001 12:00amOriginally posted by: young joon hong
ReplyThank you very much!!
Posted by Legacy on 11/29/2001 12:00amOriginally posted by: Jay Lee
Thank you for this source code...
ReplyWith this code, I can solve my problems...
But when I use this code...
First time this was not worked...
so, I edited "phdn->iItem" to "phdn->iButton".
Is it right??
If this editing was wrong, where i did mistake??
Thank you!
Posted by Legacy on 10/15/2001 12:00amOriginally posted by: J. Swindler
I had been struggling with sorting my list control items by column using the callback function method that you described, and was not having much success. I couldn't find any Microsoft documentation that said you had to find each item in the list rather than use the lparam1 and lparam2 arguments passed into the callback function. However, you explained it very well and now my control works perfectly. Great job.
Reply