?
Solved

CListCtrl sorting ...

Posted on 1997-03-10
1
Medium Priority
?
332 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

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