Sorting list based on integers in any column | CodeGuru

Sorting list based on integers in any column

Here is an extremely simple implementation for sorting, which is probably sufficient for most needs. The code determines which column in the report was clicked, converts the text data for that column into item data, then lets the list control perform its standard sorting. The code below sorts columns of integers; sorting text is even […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 6, 1998
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Here is an extremely simple implementation for sorting, which is probably
sufficient for most needs. The code determines which column in the
report was clicked, converts the text data for that column into item
data, then lets the list control perform its standard sorting. The code
below sorts columns of integers; sorting text is even simpler.

1) Use Class Wizard to map the message LVN_COLUMNCLICK to the
ListView handler function OnColumnclick. This will insert the line
ON_NOTIFY_REFLECT(LVN_COLUMNCLICK, OnColumnclick) into your derived
ListView message map. Fill in the OnColumnclick function with the code
below.

2) Add a comparison function to your derived ListView class. The
function prototype should read: static int CALLBACK CompareFunc(LPARAM,
LPARAM, LPARAM). The ListView framework calls this function to compare
ListView items (the first two function parameters). Fill in the
function with the code below.

void CMyListView::OnColumnclick(NMHDR* pNMHDR, LRESULT* pResult)
{
// Keep track of sort order and column used for sorting.

static int nSortColumn = -1;
static BOOL bSortAscending = TRUE;

// Get the index of the column header that was clicked.

NM_LISTVIEW* pNMListView = (NM_LISTVIEW*) pNMHDR;
int iColumn = pNMListView -> iSubItem;

// Reverse sort order if column was just previously clicked.

if (iColumn == nSortColumn) { bSortAscending = !bSortAscending; }
else { bSortAscending = TRUE; }
nSortColumn = iColumn;

// Set item data for each row to column value,
// since sort routine gets passed item data only.

CListCtrl& ListCtrl = GetListCtrl();
int nItems = ListCtrl.GetItemCount();

for (int item=0; item<nItems; item++)
{
CString text = ListCtrl.GetItemText(item, iColumn);

int value = atoi(text); // numeric sort
if (!bSortAscending) { value = -value; }

ListCtrl.SetItemData(item, value);
}

LPARAM lParamSort = 0;
ListCtrl.SortItems(CompareFunc, lParamSort);
}

int CALLBACK CMyListView::CompareFunc(LPARAM lParam1, LPARAM lParam2,
LPARAM lParamSort)
{
// First two parameters are item data to be compared.
// Third parameter is passed from SortItems, not used here.

if (lParam1 < lParam2) { return -1; }
if (lParam1 > lParam2) { return 1; }
return 0;
}

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.