I have a listview control in my project, runs in Visual C++ 6.0, that I want to make a sort based on column click. It works good when I have <1000 entries but when it goes over 1000 entries the sorting performance goes down. It takes about 10-15 seconds to sort the listview.

My way of making the sort is to use the SortItems() function for the listview in the OnColumnClick.

SortItems(SortFunc, (DWORD)this);

And my callback function looks like this:

int CALLBACK SortFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
	_CListCtrl* pListCtrl = (_CListCtrl*) lParamSort;

	
	LPITEM pData1 = (LPITEM)lParam1;
	LPITEM pData2 = (LPITEM)lParam2;
	

	int idx = pListCtrl->IndexFromValue(pData1->nRowID);
	int idx2 = pListCtrl->IndexFromValue(pData2->nRowID);

	CString strItem1 = pListCtrl->GetItemText(idx, pListCtrl->GetSortColumn());
	CString strItem2 = pListCtrl->GetItemText(idx2, pListCtrl->GetSortColumn());

	if(pListCtrl->GetSortAscending())
	{	
		return pListCtrl->typeSafeCompare(strItem2, strItem1) < 0;
	} else
		return pListCtrl->typeSafeCompare(strItem2, strItem1);
}

Is there any other type of sorting that has a better performance or am I stuck with this type of sorting?

appriciate all help I can get :)

brgs
Niklas

How long does it take to clear the listview control, then repopulate it with 1000 entries?

Sorting the original data without all the excess baggage of the control might just be more efficient.

Also, are display updates turned off by the control when it does the sorting? Or is it busy repainting things as it goes?

Just some guesses...

The population takes no time, it's just the sorting that takes time.

The app is busy repainting when the sorting is working.

The app is getting information from a Cobol-app in UNIX environment and if we clear the listview and send message to UNIX and we tell them to sort in this order and send a message to repopulate the listview we can cut the sorting time 2 or maybe 3 times faster. This might be a way to go but I don't think it's a good solution cause the network traffic. I believe it is more efficent to make the sotring in the listview on the clientside... if it is possible that is =)

Have you singled out that the slow performance is due to the sort operation? I suspect that the slow response may be due to the repainting event as highlighted by nille_nerholt.

Do you mind to share with us what sorting algorithm are you using? Have you tried to use the qsort function? It should not be a problem for sorting more than a thousand items.

I havn't thought about the repainting issue... is there a way to disable the repaint when doing the sorting operation?

The sorting algorithm is the one above, on the Click-event I call function SortItems(SortFunc, (DWORD)this); and that triggers the callback function with the sorting.

I did a test and turned WM_SETREDRAW off but there is still a performance problem. So I think I have isolated the sorting issue as much as a can... any thoughts?

Try to hide the listview control from the window, sort, and then show the listview back again. Windows does not bother repainting hidden controls.

Is the sorting now faster?
If yes, then it is true that the repainting is slow.
If no, then it is your sorting itself is slow, and needs to be optimized further.

If you move your listview's data into dynamic arrays and sort the data there, you may solve both these two problems at once. You will get no repainting issues and the sorting will be faster.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.