Please read the following pages
http://codeguru.earthweb.com/listview/sort_on_col.shtml
Sorting the list based on text in any column
If not enought, you can find more on sorting of ListCtrl in the following URL:
http://codeguru.earthweb.com/listview/index.shtml
http://codeguru.earthweb.com/listview/sort_on_col.shtml
Sorting the list based on text in any column
If not enought, you can find more on sorting of ListCtrl in the following URL:
http://codeguru.earthweb.com/listview/index.shtml
(注:出自本去論壇,我記不清是哪個貼了)
struct CompareFunc_lParamSort
{
CListCtrl * pl; //指向ListView控件的指針
int idCol; //指向ListView的列
BOOL isInc; //排序方式(升序為TRUE,降序為FALSE)
BOOL isStr; //是否按字符排序
}; static int CALLBACK SortFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);
int CALLBACK CxxxDlg::SortFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParam3)
{
CompareFunc_lParamSort * pparam;
CString cstr,cstr2;
int ret;
pparam=(CompareFunc_lParamSort *)lParam3;
cstr=pparam->pl->GetItemText(lParam1,pparam->idCol);
cstr2=pparam->pl->GetItemText(lParam2,pparam->idCol);
if(pparam->isStr)
ret=strcmp(cstr,cstr2);
else
ret=atof(cstr)<=atof(cstr2)?-1:1;
if(pparam->isInc)
return ret;
else
return -ret;
}
void CxxxDlg::OnColumnclickListProc(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pLV = (NM_LISTVIEW*)pNMHDR; CompareFunc_lParamSort sortparam;
static int idColindex=-1; //當前列號
static BOOL bInc=TRUE; //排序方式
int i;
if (pLV->iSubItem==idColindex) //如果是當前列,則改變排序方式
bInc=!bInc;
else{ //如果不是當前列,則重置列號,並設為升序排序
idColindex=pLV->iSubItem;
bInc=TRUE;
} i=lview.GetItemCount();
while(i--)lview.SetItemData(i,i); //注意此句:ItemData被排序操作使用
sortparam.pl=&m_lview;
sortparam.idCol=idColindex;
sortparam.isInc=bInc;
sortparam.isStr=TRUE;
m_lview.SortItems(SortFunc,(DWORD)&sortparam);
*pResult = 0;
}