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 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;
}



Comments

  • A problem...if the "itemdata" is already utilized to save some other information then...??

    Posted by aniketd on 04/26/2005 01:25am

    Yes, this is an helpful example but what if the listview control's "itemdata" needs to store some other information (e.g. Item Text Formatting information) in that case we cannot use the for loop to set the itemdata to item number... 
    
    for (int item=0; item

    Reply
  • How to sort text columns?

    Posted by Legacy on 02/22/1999 12:00am

    Originally posted by: varsh

    Sorting of integer columns works fine.I want to sort text columns when clicked on the column header using the
    SetItemData() and SortItems() method. How can i do?
    

    Reply
  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Wednesday, September 24, 2014 8:00 AM - 9:00 AM PDT According to a recent Forrester Research report, many companies are choosing low-code platforms over traditional programming platforms, due to the speed with which low-code apps can be assembled and tested. With customer-facing applications on the rise, traditional programming platforms simply can't keep up with the "short schedules and rapid change cycles" required to develop these applications. Check out this upcoming webinar and join Clay Richardson from …

  • Live Event Date: September 10, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild". This loop of continuous delivery and continuous feedback is …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds