MFC List Control sorting

How do I go about sorting items in a list control,
All columns contain strings/dates, I have searched a lot but all references are pretty old and outdated.
I want to start with sorting just 1 column that contains strings (could be ascending or descending).
I can see OnColumnClick message, in there I need to call SortItems() function which requires a call back to my compare logic.
Please help

1
2
3
4
5
6
7
8
9
static int CALLBACK WORD_SortRegular1(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
	CListCtrl* ListControl = (CListCtrl*)lParamSort;
	CString String1 = ListControl->GetItemText(lParam1, 1);
	CString String2 = ListControl->GetItemText(lParam2, 1);
	MessageBox(NULL, String1, String2, MB_OK);//String1 and String 2 are always same
        //somehow the lParam1 and lParam2 return same value
	return strcmp(String1, String2);
}
Last edited on
Yes I have been trying to get this to work with my project

https://www.codeproject.com/Articles/1360/Sort-List-Control

I am unable to add this to my own project in 2013 Visual studio, any help will be really appreciated.

According to the article usage is pretty simple
"How to use it
Look at the example to see how it is used. You need to add the files SortListCtrl.cpp/h and SortHeaderCtrl.cpp/h to your project, then associate a CSortListCtrl variable with your list control (you can do this with ClassWizard)."

Problem is I already have associated a variable to my list control, how do I go about it now?
I cannot associate multiple variable to same list control.

UPDATE:
I was able to get it to work with my project however big drawback is that I am forced to use functions provided by the author to add items to list control (these functions have limitations)
I cannot update a specific item in listcontrol, is work around it?
Last edited on
If you have a list control already you need to change the type to CSortListCtrl.
Not sure if the ClassWizard can do it, maybe you have to change it in the code. Without seeing the code it's difficult to give more precise advice.
Can you upload the complete project somewhere, maybe dropbox ?
I have added variable with "CSortListCtrl" type and its working now, problem is
I cannot use default functions of list control like SetItemText (program fails and closes) and I must use custom functions given by the author to edit/add to my list control.
Is there any solution for this?

Example:
//add column heading
m_list.SetHeadings("name,50;age,50");// used instead of InsertColumn()
//add item
*m_list.AddItem("a", "b");//used instead of SetItemText()

I would like to use the default functions instead of author's.

Last edited on
Since CSortlistCtrl inherits from CListCtrl you can call the functions from the base class.
like SetItemText (program fails and closes)
Probably you do sth. wrong.
What error msg did you get?
Not getting any error message, the program just crashes.
> Not getting any error message, the program just crashes.
So run it in the debugger then.
At least you'll get a stack trace of how you arrived at the crash, and the chance to inspect relevant variables to see what isn't set up correctly.
Topic archived. No new replies allowed.