|
// ListCtrlSortClass.cpp: implementation of the CListCtrlSortClass class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ListCtrlSortClass.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CListCtrlSortClass::CListCtrlSortClass(CListCtrl *pList, const int nCol)
{
m_pList = pList;
ASSERT(m_pList);
int totItems = m_pList->GetItemCount();
// Replace All Item Data with pointer to CSortItem class
for (int i = 0; i < totItems; i++)
{
DWORD dw = m_pList->GetItemData(i);
CString txt = m_pList->GetItemText(i, nCol);
m_pList->SetItemData(i, (DWORD) new CSortItem(dw, txt));
}
}
CListCtrlSortClass::~CListCtrlSortClass()
{
// Reset Data value for All item and delete CSortItem pointer
ASSERT(m_pList);
int totItems = m_pList->GetItemCount();
for (int i = 0; i < totItems; i++)
{
CSortItem *pItem = (CSortItem*)(m_pList->GetItemData(i));
ASSERT(pItem);
m_pList->SetItemData(i, pItem->m_dw);
delete pItem;
}
}
void CListCtrlSortClass::Sort(BOOL bAsc, SortDataType dtType)
{
// Prepare lParamSort: value == dtType,
// with sign positive for ascending sort order, negative - descending
long lParamSort = bAsc ? dtType : -dtType;
m_pList->SortItems(Compare, lParamSort);
}
int CALLBACK CListCtrlSortClass::Compare(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
CSortItem *pItem1 = (CSortItem*)lParam1;
CSortItem *pItem2 = (CSortItem*)lParam2;
ASSERT(pItem1 && pItem2);
int nOrder = (lParamSort < 0) ? -1 : 1;
SortDataType dtType = (SortDataType) (lParamSort * nOrder); // get rid of sign
switch (dtType)
{
case SortDataType::dtINT:
return ( _ttol(pItem1->m_txt) - _ttol(pItem2->m_txt)) * nOrder;
case SortDataType::dtDEC:
return (int) ((atof(pItem1->m_txt) - atof(pItem2->m_txt)) * nOrder);
case SortDataType::dtSTRING:
return pItem1->m_txt.Compare(pItem2->m_txt) * nOrder;
case SortDataType::dtSTRINGNOCASE:
return pItem1->m_txt.CompareNoCase(pItem2->m_txt) * nOrder;
case SortDataType::dtDATETIME:
{
COleDateTime time1, time2;
if (time1.ParseDateTime(pItem1->m_txt) && time2.ParseDateTime(pItem2->m_txt))
return ((time1 == time2) ? 0 : (time1 < time2 ? -1 : 1)) * nOrder;
}
default:
ASSERT("Error: attempt to sort a column without type.");
return 0;
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
LI is a architect of several database-centric tools and technologies. He has been programming since 1995. He is a Microsoft Certified Solution Developer (MCSD), MCSD.NET, SCJP, SCJD and OCDBA.
His programming experience includes C/C++, C#, MFC, ASP, VB and Perl. He has worked on Solaris, AIX, HPUX and various Windows, and found Windows is the easiest one to work with.
He has over 8 years of database experiences in Oracle, SQL Server, DB2 and other DBMS.
LI co-founded AgileInfoSoftware LLC (http://www.agileinfollc.com) in 2003. He is responsible for the overall vision and development strategy of the company.