Solved

CListCtrl sorting ...

Posted on 1997-03-10
1
326 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 50 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now