Solved

CListCtrl sorting ...

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

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

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 clear a vector as well as how to detect empty vectors in C++.
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.

732 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