Click to See Complete Forum and Search --> : Sorting Listview collumns!?


July 7th, 1999, 02:05 AM
Hi

Could someone please post a piece of code to show how to sort strings as well as integers, depending on the collumn header clicked upon.
Please add comments. Cut & paste from a project of yours, if not too much to ask.

Thank you.

July 7th, 1999, 06:34 AM
LVS_SORTING. BUTTON CLICK NMHEADER *PLK;
PLK->iButton.

July 7th, 1999, 06:55 AM
plz Explain what you mean.

Thank you.

July 7th, 1999, 09:25 AM
I've cut a bit out of my project for you, I hope there is enough.
You can ignore the sort images, just remove the code.
NB: Use the wizard to generate the OnColumnclick function.


In header file ..........

typedef struct
{
CListCtrl* pListCtrl;
int iCol;
BOOL bSortText;
BOOL bAscendOrder;
} SortInfoT;

// Attributes
public:
int m_iNofCols;
SortInfoT m_sortInfo;
BOOL m_bColTypeText[20];
CSortableHeaderCtrl m_ctlHeaderCtrl;

// Operations
public:
static int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);




in cpp file ............

void CErrorView::OnColumnclick(NMHDR* pNMHDR, LRESULT* pResult)
{
// Keep track of sort order and column used for sorting.
static int s_iSortColumn = -1;
static BOOL s_bSortAscending = TRUE;
int idx;

// Get the index of the column header that was clicked.
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*) pNMHDR;
int iColumn = pNMListView -> iSubItem;
int iItems;
CListCtrl& ListCtrl = GetListCtrl();

// Reverse sort order if column was just previously clicked.
if (iColumn == s_iSortColumn)
s_bSortAscending = !s_bSortAscending;
else
s_bSortAscending = TRUE;

s_iSortColumn = iColumn; // Set item data for each row to column value,

// since sort routine gets passed item data only.
iItems = ListCtrl.GetItemCount();

// Set all icons to the default
for (idx=0; idx<m_iNofCols; idx++)
m_ctlHeaderCtrl.SetSortImage( idx, CSortableHeaderCtrl::SORT_DIAMOND);

if (m_sortInfo.iCol == iColumn)
m_sortInfo.bAscendOrder = !m_sortInfo.bAscendOrder;
else
m_sortInfo.bAscendOrder = TRUE;

if (m_sortInfo.bAscendOrder == TRUE)
m_ctlHeaderCtrl.SetSortImage( iColumn, CSortableHeaderCtrl::SORT_UP );
else
m_ctlHeaderCtrl.SetSortImage( iColumn, CSortableHeaderCtrl::SORT_DOWN );

m_sortInfo.iCol = iColumn;
m_sortInfo.pListCtrl = &ListCtrl;
m_sortInfo.bSortText = m_bColTypeText[iColumn];

if (m_sortInfo.bSortText)
{ // Sort Text
for (int item=0; item<iItems; item++)
{
ListCtrl.SetItemData(item, item);
}
}
else
{ // Sort Integers
CString sText;
int iValue;
for (int item=0; item<iItems; item++)
{
sText = ListCtrl.GetItemText(item, iColumn);
iValue = atoi(sText); // numeric sort
ListCtrl.SetItemData(item, iValue);
}
}

ListCtrl.SortItems(CompareFunc, (DWORD)&m_sortInfo);
}

int CALLBACK CErrorView::CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{ // First two parameters are item data to be compared.
// Third parameter is sort information.

int iItem1 = (int) lParam1;
int iItem2 = (int) lParam2;
SortInfoT* pSortInfo = (SortInfoT*)lParamSort;

if (pSortInfo->bSortText)
{
CString s1 = pSortInfo->pListCtrl->GetItemText(iItem1, pSortInfo->iCol);
CString s2 = pSortInfo->pListCtrl->GetItemText(iItem2, pSortInfo->iCol);


if(pSortInfo->bAscendOrder)
return s1.Compare(s2);
else
return s2.Compare(s1);
}
else
{ // Sort Integers
if(pSortInfo->bAscendOrder)
return iItem1 - iItem2;
else
return iItem2 - iItem1;
}
}

Jeff Nuttall

PS I don't know what happend to the tabs, but at least the line returns are there.

July 8th, 1999, 03:01 AM
Just bloody marvelous!

Cheerio