Click here to Skip to main content
11,487,401 members (72,013 online)
Click here to Skip to main content

Example of CListCtrl::SortItems(...) in MSDN

, 27 Feb 2002 157.5K 35
Rate this:
Please Sign up or sign in to vote.
The given example in the documentation of CListCtrl::SortItems(...) shows us exactly the WRONG way of using the function.
<!-- Add the rest of your HTML here -->

If we look at the documentation of CListCtrl::SortItems(...) in MSDN we find the following example of how to sort the items in reverse alphabetical order:

// Sort the item in reverse alphabetical order.
static int CALLBACK 
MyCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
   // lParamSort contains a pointer to the list view control.
   CListCtrl* pListCtrl = (CListCtrl*) lParamSort;
   CString    strItem1 = pListCtrl->GetItemText(lParam1, 0);
   CString    strItem2 = pListCtrl->GetItemText(lParam2, 0);

   return strcmp(strItem2, strItem1);
}

void snip_CListCtrl_SortItems()
{
   // The pointer to my list view control.
   extern CListCtrl* pmyListCtrl;

   // Sort the list view items using my callback procedure.
   pmyListCtrl->SortItems(MyCompareProc, (LPARAM) pmyListCtrl);
}
This approach obviously does not work and here is why: The parameters of MyCompareProc(...) - lParam1 and lParam2 are not the index of the items but their 32-bit associated value.

There is a second problem: even if we set the 32-bit associated value of each item to be the same as the item's index this example will still not work. This is because the items will shift positions after each call to MyCompareProc(...) and the 32-bit associated values will no longer represent the item's index.

The function CListCtrl::SortItems(...) is only usefull if we store a pointer in the 32-bit associated value and we want to sort the items by some value that comes from that pointer.

One easy way of implementing reverse alphabetical sort in CListCtrl derived class is using an STL set:

#pragma warning(disable : 4786)
#include <functional>
#include <set>

struct LVITEM_less : public std::binary_function<LVITEM*, LVITEM*, bool>
{
    bool operator()(const LVITEM* pItem1, const LVITEM* pItem2) const
    {
        CString strItem1(pItem1->pszText);
        CString strItem2(pItem2->pszText);

        return (strItem1 < strItem2);
    }
};

void CSortableListCtrl::SortItemsDescending()
{
    // Sort all items in descending aphabetical order using an STL set
    typedef std::set<LVITEM*, LVITEM_less> ItemSet;
    ItemSet setItems;
    int iCount = GetItemCount();
    for (int i = 0; i < iCount; i++)
    {
        LVITEM* pLVI = new LVITEM();
        ::memset(pLVI, 0, sizeof(LVITEM));
        pLVI->iItem = i;
        pLVI->mask = LVIF_IMAGE | LVIF_INDENT | LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
        pLVI->pszText = new TCHAR[1024];
        pLVI->cchTextMax = 1024;
        GetItem(pLVI);

        setItems.insert(pLVI);
    }

    // Remove all items from the list control
    DeleteAllItems();

    // Put the items back in the list control in reverse order
    int iIndex = 0;
    for (ItemSet::reverse_iterator it = setItems.rbegin(); it != setItems.rend(); ++it)
    {
        (*it)->iItem = iIndex++;
        InsertItem(*it);
        delete [] (*it)->pszText;
        delete *it;
    }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Ivor S. Sargoytchev
Software Developer (Senior)
Canada Canada
No Biography provided

Comments and Discussions

 
QuestionIgnoring the repeatations Pin
At_work3-Jun-08 23:06
memberAt_work3-Jun-08 23:06 
QuestionExplanation Pin
tom groezer25-May-07 13:08
membertom groezer25-May-07 13:08 
GeneralMuch simpler solution!! Pin
Andy Bray4-Aug-06 0:30
memberAndy Bray4-Aug-06 0:30 
Generalauthor does not understand how to use the sort issue at all Pin
includeh1016-Jun-06 5:19
memberincludeh1016-Jun-06 5:19 
GeneralI use it with NO problem Pin
RancidCrabtree24-Aug-05 10:36
memberRancidCrabtree24-Aug-05 10:36 
GeneralRe: I use it with NO problem Pin
Leonhardt Wille11-May-06 22:29
memberLeonhardt Wille11-May-06 22:29 
GeneralError correction of MSDN Sample... Pin
martin.compel22-May-03 7:18
membermartin.compel22-May-03 7:18 
GeneralRe: Error correction of MSDN Sample... Pin
Anonymous27-Aug-03 6:23
sussAnonymous27-Aug-03 6:23 
GeneralRe: Error correction of MSDN Sample... Pin
Priya_Sundar16-Apr-08 3:50
memberPriya_Sundar16-Apr-08 3:50 
GeneralRe: Error correction of MSDN Sample... Pin
Anonymous15-Oct-03 4:58
sussAnonymous15-Oct-03 4:58 
GeneralRe: Error correction of MSDN Sample... Pin
cibercop16-Aug-05 6:55
membercibercop16-Aug-05 6:55 
Generalmore simple example . Pin
radezz15-Sep-05 13:28
memberradezz15-Sep-05 13:28 
QuestionHow to: make lParam1 and lParam2 being indices Pin
Geert Delmeiren8-Apr-02 2:14
memberGeert Delmeiren8-Apr-02 2:14 
AnswerRe: How to: make lParam1 and lParam2 being indices Pin
rsmenezes25-Aug-02 8:20
memberrsmenezes25-Aug-02 8:20 
GeneralAlternative to slow SortItems Pin
Tim Kosse28-Feb-02 8:19
memberTim Kosse28-Feb-02 8:19 
GeneralRe: Alternative to slow SortItems Pin
Ivor S. Sargoytchev28-Feb-02 8:26
memberIvor S. Sargoytchev28-Feb-02 8:26 
GeneralThis was the Introduction here some tips... Pin
KarstenK28-Feb-02 1:29
memberKarstenK28-Feb-02 1:29 
QuestionSolution? Pin
Davide Calabro27-Feb-02 22:33
memberDavide Calabro27-Feb-02 22:33 
AnswerRe: Solution? Pin
Chris Maunder28-Feb-02 5:33
memberChris Maunder28-Feb-02 5:33 
GeneralSorry... Pin
Davide Calabro28-Feb-02 6:37
memberDavide Calabro28-Feb-02 6:37 
GeneralRe: Solution? Pin
David Wulff28-Feb-02 14:30
memberDavid Wulff28-Feb-02 14:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 28 Feb 2002
Article Copyright 2002 by Ivor S. Sargoytchev
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid