Sorting using CListCtrl::SortItems
-
I'm having a problem using this function of the MFC List Control class I've read the article : Example of CListCtrl::SortItems(...) in MSDN By Ivor S. Sargoytchev Example of CListCtrl::SortItems(...) in MSDN[^] And am trying out his example Callback funtion this way:
static int CALLBACK CompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
// lParamSort contains a pointer to the list view control.
CListCtrl* pListCtrl = (CListCtrl*) lParamSort;
CString strItem1 = pListCtrl->GetItemText(lParam1, 0);
CString strItem2 = pListCtrl->GetItemText(lParam2, 0);return _tcscmp(strItem1, strItem2); // This should do ascending Sort, I believe?
}Relevant Code:
LVCOLUMN lvColumn;
int nCol =0;lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
lvColumn.fmt = LVCFMT_LEFT;
lvColumn.cx = 120;
lvColumn.pszText = ColumnHeading[0]; //<-- Global Enum list of titles
nCol = m_MainList.InsertColumn(0, &lvColumn);int NextIndex = nCol;
.
.
.
TCHAR Output[50]={0};
while ()
{LVITEM lvItem;
lvItem.mask = LVIF_TEXT;
lvItem.iItem = NextIndex;
lvItem.iSubItem = 0;
lvItem.pszText = Output;
NextIndex = m_MainList.InsertItem(&lvItem);
}m_MainList.SortItems(CompareProc, (LPARAM)&m_MainList);
m_MainList is the Control variable for a List Control box I placed on a dialog I put a breakpoint inside the Sort Function
CompareProc
and found that lParam1 and lParam2 are always 0 constantly and never increasing. I have been told it could be a pointer issue, but I don't get it. Can anyone help? Thanks -
I'm having a problem using this function of the MFC List Control class I've read the article : Example of CListCtrl::SortItems(...) in MSDN By Ivor S. Sargoytchev Example of CListCtrl::SortItems(...) in MSDN[^] And am trying out his example Callback funtion this way:
static int CALLBACK CompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
// lParamSort contains a pointer to the list view control.
CListCtrl* pListCtrl = (CListCtrl*) lParamSort;
CString strItem1 = pListCtrl->GetItemText(lParam1, 0);
CString strItem2 = pListCtrl->GetItemText(lParam2, 0);return _tcscmp(strItem1, strItem2); // This should do ascending Sort, I believe?
}Relevant Code:
LVCOLUMN lvColumn;
int nCol =0;lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
lvColumn.fmt = LVCFMT_LEFT;
lvColumn.cx = 120;
lvColumn.pszText = ColumnHeading[0]; //<-- Global Enum list of titles
nCol = m_MainList.InsertColumn(0, &lvColumn);int NextIndex = nCol;
.
.
.
TCHAR Output[50]={0};
while ()
{LVITEM lvItem;
lvItem.mask = LVIF_TEXT;
lvItem.iItem = NextIndex;
lvItem.iSubItem = 0;
lvItem.pszText = Output;
NextIndex = m_MainList.InsertItem(&lvItem);
}m_MainList.SortItems(CompareProc, (LPARAM)&m_MainList);
m_MainList is the Control variable for a List Control box I placed on a dialog I put a breakpoint inside the Sort Function
CompareProc
and found that lParam1 and lParam2 are always 0 constantly and never increasing. I have been told it could be a pointer issue, but I don't get it. Can anyone help? ThanksJJeffrey wrote:
I put a breakpoint inside the Sort Function CompareProc and found that lParam1 and lParam2 are always 0 constantly and never increasing.
When you use the
SortItems()
function, the lParam1 and lParam2 passed to theCompareProc
are not item indexes, but Item data corrsponding to each item in the list control. I must says its bit difficult to sort if you use theSortItems()
function.Sortitems()
wrapper the messageLVM_SORTITEMS
. But there is another message calledLVM_SORTITEMSEX
, which will work as you expected. So instead ofm_MainList.SortItems(CompareProc, (LPARAM)&m_MainList);
, callListView_SortItemsEx( m_MainList.m_hWnd, CompareProc, (LPARAM)&m_MainList );
-
JJeffrey wrote:
I put a breakpoint inside the Sort Function CompareProc and found that lParam1 and lParam2 are always 0 constantly and never increasing.
When you use the
SortItems()
function, the lParam1 and lParam2 passed to theCompareProc
are not item indexes, but Item data corrsponding to each item in the list control. I must says its bit difficult to sort if you use theSortItems()
function.Sortitems()
wrapper the messageLVM_SORTITEMS
. But there is another message calledLVM_SORTITEMSEX
, which will work as you expected. So instead ofm_MainList.SortItems(CompareProc, (LPARAM)&m_MainList);
, callListView_SortItemsEx( m_MainList.m_hWnd, CompareProc, (LPARAM)&m_MainList );
-
Thank you. That solved it. So it's mostly Microsoft's fault then? For coming up with that stupid function? Really. Thanks. Was working on trying to make it work for a day now.
You are welcome.
JJeffrey wrote:
So it's mostly Microsoft's fault then? For coming up with that stupid function?
During te sorting process, the list-view contents are unstable. So in the intial versions of common control, I guess they might have felt some difficulty is passing the item indexes to the compare function. Later from the comctl32.dll version 5.80, they added the support for the LVM_SORTITEMSEX message. They might have found some work around later.. :)
-
I'm having a problem using this function of the MFC List Control class I've read the article : Example of CListCtrl::SortItems(...) in MSDN By Ivor S. Sargoytchev Example of CListCtrl::SortItems(...) in MSDN[^] And am trying out his example Callback funtion this way:
static int CALLBACK CompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
// lParamSort contains a pointer to the list view control.
CListCtrl* pListCtrl = (CListCtrl*) lParamSort;
CString strItem1 = pListCtrl->GetItemText(lParam1, 0);
CString strItem2 = pListCtrl->GetItemText(lParam2, 0);return _tcscmp(strItem1, strItem2); // This should do ascending Sort, I believe?
}Relevant Code:
LVCOLUMN lvColumn;
int nCol =0;lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
lvColumn.fmt = LVCFMT_LEFT;
lvColumn.cx = 120;
lvColumn.pszText = ColumnHeading[0]; //<-- Global Enum list of titles
nCol = m_MainList.InsertColumn(0, &lvColumn);int NextIndex = nCol;
.
.
.
TCHAR Output[50]={0};
while ()
{LVITEM lvItem;
lvItem.mask = LVIF_TEXT;
lvItem.iItem = NextIndex;
lvItem.iSubItem = 0;
lvItem.pszText = Output;
NextIndex = m_MainList.InsertItem(&lvItem);
}m_MainList.SortItems(CompareProc, (LPARAM)&m_MainList);
m_MainList is the Control variable for a List Control box I placed on a dialog I put a breakpoint inside the Sort Function
CompareProc
and found that lParam1 and lParam2 are always 0 constantly and never increasing. I have been told it could be a pointer issue, but I don't get it. Can anyone help? ThanksFor future reference, when adding items to the control, you'll also need to call
SetItemData()
. This is becauseSortItems()
internally callsGetItemData()
(or an equivalent) before calling your comparison routine."Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons