API for MFC Sorting Controls
SortListCtrl

Copyright © 2016 by David Wincelberg

Window Styles

Recommended report view
Optional sort ascending or sort descending

ListCtrlData

CListCtrlData (CListCtrl* pListCtrl, LPARAM lParam = 0);

constructor

pListCtrl pointer to a list control
lParam application-specific data

CListCtrlData (int nCols = 0, LPARAM lParam = 0);

constructor

nCols number of columns in the list control
lParam application-specific data

CListCtrlData (CStringArray& saCols, LPARAM lParam = 0);

constructor

saCols reference to a string array
lParam application-specific data

void SetAt (int nCol, LPCTSTR newElement);

Sets the string at the specified column to the specified value.

nCol column number
newElement pointer to a string

virtual CString GetAt (int nCol) const;

Returns the string at the specified column.

nCol a column in the list control

void SetLParam (LPARAM lParam);

Sets the application-specific data to the specified value.

lParam application-specific data

LPARAM GetLParam() const;

Returns the application-specific data.

ListCtrlOrder

CListCtrlOrder (CStringOrder* pStringOrder = nullptr, bool bHandleDelete = true);

constructor

pStringOrder pointer to a string-order object
bHandleDelete specifies whether to delete this string-order object

void SetStringOrder (CStringOrder* pStringOrder, bool bHandleDelete = true);

Sets the string-order to the specified value.

pStringOrder pointer to a string-order object
bHandleDelete specifies whether to delete this string-order object

bool ValidStringOrder()

Returns true if the string-order object is not null.

CStringOrder* GetStringOrder();

Returns the string-order object.

void SetValues (int nCol, bool bReverse);

Sets values for sorting.

nCol a column in the list control
bReverse flag for sorting in reverse order

virtual int Compare (LPARAM lParam1, LPARAM lParam2);

Returns an integer that specifies order of the strings based on the corresponding data items.

lParam1 pointer to the first data item
lParam2 pointer to the second data item

SortListCtrl

CSortListCtrl (CListCtrlOrder* pListCtrlOrder = nullptr, bool bHandleDelete = true);

constructor

pListCtrlOrder pointer to a sort-order object
bHandleDelete specifies whether to delete this sort-order object

CSortListCtrl (CStringOrder* pStringOrder, bool bHandleDeleteInner = true, bool bHandleDeleteOuter = true);

constructor

pListCtrlOrder pointer to a string-order object
bHandleDeleteInner specifies whether to delete this string-order object
bHandleDeleteOuter specifies whether to delete the sort-order object

void SetSortOrder (CListCtrlOrder* pListCtrlOrder, bool bHandleDelete = true);

Sets the sort-order to the specified value.

pListCtrlOrder pointer to sort-order object
bHandleDelete specifies whether to delete this sort-order object

void SetStringOrder (CStringOrder* pStringOrder, bool bHandleDelete = true);

Sets the string-order to the specified value.

pStringOrder pointer to string-order object
bHandleDelete specifies whether to delete this string-order object

void Sort (int nCol = 0, bool bReverse = false);

Sorts the specified column in the specified direction.

nCol a column in the list control
bReverse flag for sorting in reverse order

void SetColSorted (int nCol = 0, bool bReverse = false);

Specifies that a column is sorted in the specified direction.

nCol a column in the list control
bReverse flag for sorting in reverse order

void SuspendColSort (bool bSuspend);

Suspends or resumes allowing column sorting.
Note: The column buttons can still be pressed when column sorting is suspended. To deactivate them, use something like the following:
m_lc.GetHeaderCtrl()->EnableWindow (!bSuspend);

bSuspend flag for suspending column sorting

int InsertItem (int nItem, LPCTSTR lpszItem, CListCtrlData* pListCtrlData = nullptr);

int InsertItem (int nItem, LPCTSTR lpszItem, int nImage, CListCtrlData* pListCtrlData = nullptr);

int InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam, CListCtrlData* pListCtrlData = nullptr);

int InsertItem (LVITEM* pItem, CListCtrlData* pListCtrlData = nullptr);

Same as for CListCtrl with an optional parameter for specifying a list-control data item

BOOL DeleteItem (int nItem);
BOOL SetItemData (int nItem, DWORD dwData);
DWORD GetItemData (int nItem) const;

Same as for CListCtrl

int SetItemDataObjPtr (int nIndex, CListCtrlData* pListCtrlData);

Stores user data as a pointer to a CListCtrlData object.
Copies the data values from the old data object and deletes it. pListCtrlData should be from a subclass of CListCtrlData.

nIndex index into the list control
pListCtrlData pointer to a data object

CListCtrlData* GetItemDataObjPtr (int nIndex) const;

Retrieves user data as a pointer to a CListCtrlData object.

nIndex index into the list control