MainDlg_OnListColumnClick will modify the sort orientation according to the global variable that holds the column information (will set the sort order, priority, or, if the sort became inactive, will affect the priorities of the following column(s) in the sort order). BOOL
MainDlg_OnListColumnClick(HWND hWnd, LPARAM lParam)
{
LPNMLISTVIEW lpNMLV;
int iColumnClicked;
lpNMLV = (LPNMLISTVIEW)lParam;
iColumnClicked = lpNMLV->iSubItem;
MainDlg_SetSortOnColumn(hWnd, iColumnClicked);
MainDlg_SortList(hWnd);
return FALSE;
}
Let's take a closer look:
VOID
MainDlg_SetSortOnColumn(HWND hWnd, int iColIdx)
{
int iNextPrior;
int iSortOrder;
HWND hwndLV;
HWND hwndHdr;
hwndLV = GetDlgItem(hWnd, IDLV_REPORT);
hwndHdr = ListView_GetHeader(hwndLV);
MainDlg_GetColumnSortPriority(hWnd, iColIdx, &iNextPrior);
if(iNextPrior == LVSORTPRIORITY_NONE)
{
MainDlg_FindNextSortPriority(hWnd, &iNextPrior);
MainDlg_SetColumnSortOrder(hWnd, iColIdx, LVORDER_ASCENDING);
MainDlg_SetColumnSortPriority(hWnd, iColIdx, iNextPrior);
}
else
{
MainDlg_GetColumnSortOrder(hWnd, iColIdx, &iSortOrder);
if(iSortOrder == LVORDER_ASCENDING)
{
MainDlg_SetColumnSortOrder(hWnd, iColIdx, LVORDER_DESCENDING);
}
else {
MainDlg_SetColumnSortOrder(hWnd, iColIdx, LVORDER_NONE);
MainDlg_DecreasePriority(hWnd, iColIdx);
}
}
InvalidateRect(hwndHdr, NULL, TRUE);
}
The list sorting routine has nothing special. It looks on the data type it has to compare, converts the cells' data from string to the appropriate type (looking on SAMPLELISTVIEWCOLUMN nSortType member (it can be LVORDER_STRING (VT_BSTR), LVORDER_NUMERIC (VT_I4) or LVORDER_DATETIME (VT_DATE), in this sample) and performs the VARIANT comparison calling VarCmp if the conversion succeeded. If an error occurred (conversion, comparison, etc.), the items are considered equal.