 |
|
 |
The code is working correctly but its not cosidering the repeated entries in the list box.
Final list contains the item only once even if it was present more than once in the list before sorting.
My list contains two columns, its like filename and line no., so how to sort in such a way that in the resultant list both columns should remain associated with each other.
|
|
|
|
 |
|
 |
Could some1 please explain line by line as to what is happening in the code.
|
|
|
|
 |
|
 |
Just put the index value into the lParam member of each item just before calling the SortItems function.
int nCount = m_list.GetItemCount() ;
if ( nCount > 0 )
{
for ( int i=0 ; i < nCount ; i++ )
{
m_list.SetItemData( i, i ) ;
}
m_list.SortItems(CompareFunc, (LPARAM)&m_list);
}
Works a treat
|
|
|
|
 |
|
|
 |
|
 |
Since lParam1 and lParam2 are not the index of the items but their 32-bit associated value, simply take advantage of that fact.
If you are sorting on the text of the items, you have the original problem. But if you are sorting on data that is pointed to by lParam1 and lParam2, then there is no need to know anything about the control itself.
|
|
|
|
 |
|
 |
Correct.
Another workaround would be to set the lParam to lpszText when inserting the Item:
LVITEM LVI;
LVI.iItem = iIndex;
LVI.mask = LVIF_IMAGE | LVIF_INDENT | LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
LVI.lParam=LVI.pszText = new TCHAR[1024];
LVI.cchTextMax = 1024;
InsertItem(&LVI);
|
|
|
|
 |
|
 |
Hi,
here is the solution for wrong MSDN SortItem() sample:
Point is in use of FindItem() method
int CALLBACK CSCListCtrl::CompareFunction( LPARAM lParam1, LPARAM lParam2, LPARAM lParamData )
{
CSCListCtrl* pListCtrl = (CSCListCtrl*) lParamData;
LVFINDINFO pInfo1, pInfo2;
pInfo1.flags = LVFI_PARAM;
pInfo2.flags = LVFI_PARAM;
pInfo1.lParam = lParam1;
pInfo2.lParam = lParam2;
int ind1 = pListCtrl->FindItem(&pInfo1);
int ind2 = pListCtrl->FindItem(&pInfo2);
CString strItem1 = pListCtrl->GetItemText(ind1, pListCtrl->m_iSortColumn);
CString strItem2 = pListCtrl->GetItemText(ind2, pListCtrl->m_iSortColumn);
return pListCtrl->m_bSortAscending ? lstrcmp( strItem1, strItem2 ) : lstrcmp( strItem2, strItem1 );
}
void CSCListCtrl::SortColumn( int iColumn, BOOL bAscending )
{
m_iSortColumn = iColumn;
m_bSortAscending = bAscending;
VERIFY( SortItems( CompareFunction, reinterpret_cast( this ) ));
}
void CSCListCtrl::OnColumnclick(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
const int iColumn = pNMListView->iSubItem;
SortColumn( iColumn, iColumn == m_iSortColumn ? !m_bSortAscending : TRUE );
*pResult = 0;
}
Bye,
Vajco
|
|
|
|
 |
|
|
 |
|
 |
Anonymous wrote: http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B250614
Good link. Helped a lot to understand exactly what is needed. Thanks!
Priya Sundar
|
|
|
|
 |
|
 |
I am a novice. This code is not working for me. When I debug CompareFunction, why lParam1 and lParam2 are always zero at every call, thus ind1 and ind2 are equal to zero as well.
|
|
|
|
 |
|
 |
This works great, simple and to the point.
Thanks,
Rob
|
|
|
|
 |
|
 |
Originaly list should be set in alphabeticaly order if you want to reverse it, just do int CALLBACK reverseFunction(LPARAM lparam1,LPARAM lparam2,LPARAM lParamData) { if(lparam1 < lparam2) return -1; else retutn 1; } someone got more simple way?.... that means that 32-bit lparam value has ascending (or descending) order, whatever... change the "<" to ">" and you will have prevoius order of the list. radezz
|
|
|
|
 |
|
 |
As someone remarked in a comment of the CP article Sort List Control you can use the macro ListView_SortItemsEx and then the lParam1 and lParam2 ARE the indices indeed.
|
|
|
|
 |
|
 |
or else you can explicitly set the lparam before calling the compare function with the original SortItems as in
for (int i=0;i < this->GetItemCount();i++)
{
this->SetItemData(i, (DWORD)i);
}
VERIFY( SortItems( CompareFunction, (LPARAM)( this ) ));
|
|
|
|
 |
|
 |
SortItems is way too slow when the list gets large and if the list isn't already partially sorted. It looks like if Bubblesort is used to sort the list.
So I sort my list controls using another algorithm: Quicksort
In order to use quicksort, you have to store all required data outside the list (or just export the entries before sorting) in an array or something compareable.
Now Quicksort the array and rebuild the list (delete all entries and readd them from the sorted array)
This is in most cases faster than SortItems.
To increase speed even further, use lists with the LVS_OWNERDATA style set.
Signature? I've no need for it!
|
|
|
|
 |
|
 |
Inserting all items in a set would be even faster than using Quicksort since the items are sorted as they are inserted.
Ivor S. Sargoytchev
Dundas Software
|
|
|
|
 |
|
 |
It seems to be the first part of a longer article.
But it´s okay.
The hint with the 32bit value is good, maybe it sould be a pointer to an allocated object (with new) or another idea is an indexvalue. My experience with this is, that the "compare" function is called very often, so that it is an good idea to design it short and quick.
|
|
|
|
 |
|
 |
In your "article" you show a problem, but it will be useful to other users if you show a better solution....
SoftechSoftware
Davide Calabro'
davide_calabro@yahoo.com
http://www.softechsoftware.it
|
|
|
|
 |
|
 |
This is the Bug List section. There will not necessarily be solutions (or even workarounds) to these issues.
The section has been set up so that we have a single, organised place where everyone can report bugs, and also so that we can start a dialog on the various bugs and work out a common solution.
cheers,
Chris Maunder
|
|
|
|
 |
|
 |
...it was my mistake
bye,
SoftechSoftware
Davide Calabro'
davide_calabro@yahoo.com
http://www.softechsoftware.it
|
|
|
|
 |
|
 |
Chris Maunder wrote:
This is the Bug List section. There will not necessarily be solutions (or even workarounds) to these issues.
I noticed it is actually called the "Microsoft Bug List" - are you trying to tell us something, Chris?
Okay, okay, I know the answer to this, but let me make my joke and i'll be happy.
This was a good idea to add a bug list section, now all I have to do is remember to keep checking it for regular updates!
________________
David Wulff
http://www.davidwulff.co.uk
Sonork ID: 100.9977 Dave
…
|
|
|
|
 |