Partilhar via


CListCtrl::SortItems

Sorts list view items by using an application-defined comparison function.

BOOL SortItems(
   PFNLVCOMPARE pfnCompare,
   DWORD_PTR dwData 
);

Parameters

  • [in] pfnCompare
    Address of the application-defined comparison function.

    The sort operation calls the comparison function each time the relative order of two list items needs to be determined. The comparison function must be either a static member of a class or a stand-alone function that is not a member of any class.

  • [in] dwData
    Application-defined value that is passed to the comparison function.

Return Value

true if the method successful; otherwise false.

Remarks

This method changes the index of each item to reflect the new sequence.

The comparison function, pfnCompare, has the following form:

int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);

The comparison function must return a negative value if the first item should precede the second, a positive value if the first item should follow the second, or zero if the two items are equal.

The lParam1 parameter is the 32-bit value associated with the first item that is compared, and the lParam2 parameter is the value associated with the second item. These are the values that were specified in the lParam member of the items' LVITEM structure when they were inserted into the list. The lParamSort parameter is the same as the dwData value.

This method sends the LVM_SORTITEMS message, which is described in the Windows SDK.

Example

The following is a simple comparison function that results in items being sorted by their lParam values.

// Sort items by associated lParam
int CALLBACK CListCtrlDlg::MyCompareProc(LPARAM lParam1, LPARAM lParam2, 
    LPARAM lParamSort)
{
    UNREFERENCED_PARAMETER(lParamSort);

    return (int)(lParam1 - lParam2);
}
// Sort the items by passing in the comparison function.
void CListCtrlDlg::Sort()
{
    m_myListCtrl.SortItems(&CListCtrlDlg::MyCompareProc, 0);
}

Requirements

Header: afxcmn.h

This control is supported under Windows NT 3.51 or later.

See Also

Reference

CListCtrl Class

Hierarchy Chart

CListCtrl::FindItem

CListCtrl::SortItemsEx

Other Resources

CListCtrl Members

Change History

Date

History

Reason

June 2010

Added call to SortItems.

Customer feedback.