Sorting list control
Posted on 2005-05-05
I have very basic knowledge in C/C++ and I need your help.
This shouldn't be hard for you.
I have one column list control and I would like items to appear sorted A-Z (ascending).
At first I started with SortItems and callback function.
Only first item seems to compare (dunno why, perhaps I did something wrong) as many as there is elements in the list.
For example: a,b,c,d
Compare function repeats comparing a=a.
Also, what about alternative to use qsort?
This is code I have at the moment:
// application-defined comparison function
static int CALLBACK MyCompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
// pointer to list object
CMyListBox *pList = (CListCtrl*) lParamSort;
// first item text to compare
CString strItem1 = pList->GetItemText(lParam1, 0);
// second item text to compare
CString strItem2 = pList->GetItemText(lParam2, 0);
// compare both items and return:
// - negative if item1 < item2
// - zero if item1 == item2
// - positive if item1 > item2
return strcmp(strItem2, strItem1);
void CEditObjectPageTree: nTreeAddTree()
CMyListBox *pList = (CMyListBox*)GetDlgItem(IDC_TREE_TREES);
// sort listbox items with "application-defined" comparison function
pList->SortItems(MyCompareFunc, (LPARAM) pList);