?
Solved

CListCtrl sorting ...

Posted on 1997-03-10
1
Medium Priority
?
336 Views
Last Modified: 2008-03-03
how do I set up a list control to allow simle alphanumeric
sorting when the column title button is clicked .

i.e. Just like the windows explorer columns.

Should I be using the more simpler CViewList.?
0
Comment
Question by:georgeo
1 Comment
 
LVL 1

Accepted Solution

by:
MFCGuy earned 100 total points
ID: 1161899
First, set up your ListCtrl rows to have an LParam (SetItemData()) that means something for the sort
like a pointer to the object you want to sort on.
I used an object in the example below.

Insert the following code:

in .h
-----
static int CALLBACK ListCtrlCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);

in .cpp
-------
int CALLBACK CODListCtrl::ListCtrlCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
      //If LPARAMs are invalid, return 0 (sort items are equal)
      if(lParam1 == 0 || lParam2 == 0)
            return 0;
        int column = (int) lParamSort;

      CMyObject* s1 = (CMyObject*)lParam1;
      CMyObject* s2 = (CMyObject*)lParam2;
//I'm only sorting column 0 here...
        if(column == 0)      //default - compare as CStrings
             return s1->m_aString.Compare(s2->m_aString);
        return 0;//rows matched
}

Call the function in your .cpp when the user clicks on a column
by using:
CListCtrl::SortItems(ListCtrlCompareProc, <the column that was sorted on>);

This will sort in ascending order.

Hope that gave you enough to get you going...

Regards,
MFCGuy
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question