Partilhar via


CListCtrl::SortItemsEx

Sorts the items of the current list-view control by using an application-defined comparison function.

BOOL SortItemsEx(
     PFNLVCOMPARE pfnCompare, 
     DWORD_PTR dwData
);

Parameters

Parameter

Description

[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 passed to the comparison function.

Return Value

true if this method is 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); 

This message is like LVM_SORTITEMS, except for the type of information passed to the comparison function. In LVM_SORTITEMS, lParam1 and lParam2 are the values of the items to compare. In LVM_SORTITEMSEX, lParam1 is the current index of the first item to compare and lParam2 is the current index of the second item. You can send an LVM_GETITEMTEXT message to retrieve more information about an item.

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.

Observação

During the sorting process, the list-view contents are unstable. If the callback function sends any messages to the list-view control other than LVM_GETITEM, the results are unpredictable.

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

Requirements

Header: afxcmn.h

This method is supported in Windows 2000, Windows NT 4.0 with Internet Explorer 5, Windows 98, and later.

Example

The following code example defines a variable, m_listCtrl, that is used to access the current list-view control. This variable is used in the next example.

public:
    // Variable used to access the list control.
    CListCtrl m_listCtrl; 

The following code example demonstrates the SortItemEx method. In an earlier section of this code example, we created a list-view control that displays two columns titled "ClientID" and "Grade" in a report view. The following code example sorts the table by using the values in the "Grade" column.

// The ListCompareFunc() method is a global function used by SortItemEx().
int CALLBACK ListCompareFunc(
                             LPARAM lParam1, 
                             LPARAM lParam2, 
                             LPARAM lParamSort)
{
    CListCtrl* pListCtrl = (CListCtrl*) lParamSort;
    CString    strItem1 = pListCtrl->GetItemText(static_cast<int>(lParam1), 1);
    CString    strItem2 = pListCtrl->GetItemText(static_cast<int>(lParam2), 1);

    int x1 = _tstoi(strItem1.GetBuffer());
    int x2 = _tstoi(strItem2.GetBuffer());
    int result = 0;
    if ((x1 - x2) < 0)
        result = -1;
    else if ((x1 - x2) == 0)
        result = 0;
    else
        result = 1;

    return result;
}

void CCListCtrl_s2Dlg::OnBnClickedButton1()
{
    // SortItemsEx
    m_listCtrl.SortItemsEx( ListCompareFunc, (LPARAM)&m_listCtrl );
}

See Also

Reference

CListCtrl Class

Hierarchy Chart

LVM_SORTITEMSEX

CListCtrl::SortItems

LVM_SORTITEMS

LVM_GETITEM

Other Resources

CListCtrl Members