www.pudn.com > HReportTest.rar > HListSort.cpp
// HListSort.cpp: implementation of the CHListSort class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "HListSort.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
CHListSort::CHListSort(CListCtrl* pListCtrl, const int nCol)
{
ASSERT(pListCtrl != NULL);
m_pListCtrl = pListCtrl;
int nCount = m_pListCtrl->GetItemCount();
for (int i = 0; i < nCount; i++)
{
DWORD dwData = m_pListCtrl->GetItemData(i);
CString strItemText = m_pListCtrl->GetItemText(i, nCol);
m_pListCtrl->SetItemData(i, (DWORD) new SORT_ITEM(dwData, strItemText));
}
}
CHListSort::~CHListSort()
{
ASSERT(m_pListCtrl != NULL);
int nCount = m_pListCtrl->GetItemCount();
for (int i = 0; i < nCount; i++)
{
SORT_ITEM* pItem = (SORT_ITEM*) m_pListCtrl->GetItemData(i);
ASSERT(pItem);
m_pListCtrl->SetItemData(i, pItem->m_dwData);
delete pItem;
}
}
void CHListSort::Sort(BOOL bAsc, DATA_TYPE eType)
{
long lParamSort = eType;
if (!bAsc)
{
lParamSort *= -1;
}
m_pListCtrl->SortItems(Compare, lParamSort);
}
int CALLBACK CHListSort::Compare(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
SORT_ITEM* item1 = (SORT_ITEM*)lParam1;
SORT_ITEM* item2 = (SORT_ITEM*)lParam2;
ASSERT(item1 && item2);
short sOrder = lParamSort < 0 ? (short) -1 : (short) 1;
DATA_TYPE eType = (DATA_TYPE) (lParamSort * sOrder);
COleDateTime t1, t2;
switch (eType)
{
case DT_INT:
return ( _ttol( item1->m_strItemText ) - _ttol( item2->m_strItemText )) * sOrder;
case DT_DEC:
#ifdef _UNICODE
{
char szText1[ 256 ];
char szText2[ 256 ];
::WideCharToMultiByte( CP_ACP, 0, item1->m_strItemText, -1, szText1,
255, NULL, NULL );
::WideCharToMultiByte( CP_ACP, 0, item2->m_strItemText, -1, szText2,
255, NULL, NULL );
return ( atof( szText1 ) < atof( szText2 ) ? -1 : 1) * sOrder;
}
#else
return ( atof ( item1->m_strItemText ) < atof( item2->m_strItemText ) ? -1 : 1) * sOrder;
#endif
case DT_DATETIME:
if( t1.ParseDateTime( item1->m_strItemText ) && t2.ParseDateTime( item2->m_strItemText )) {
return (t1 < t2 ? -1 : 1 ) * sOrder;
}
return 0;
case DT_STRING:
return item1->m_strItemText.CompareNoCase( item2->m_strItemText ) * sOrder;
default:
ASSERT(0);
return 0;
}
}