您的位置:首页 > 其它

自定义结构体vector排序

2011-07-12 15:27 267 查看
// 列表行内容

typedef struct _LIST_ROW

{

int iSeq; // 序号

DWORD dwId; // 表中的Id号

TCHAR szEmp[64]; // 用户名

TCHAR szIP[20]; // IP

TCHAR szMAC[20]; // MAC

TCHAR szType[32]; // 类型

TCHAR szValid[32]; // 有效期

TCHAR szBeg[32]; // 开始时间

TCHAR szEnd[32]; // 结束时间

TCHAR szRemark[32]; // 备注

int iMode; // 工作模式

}LIST_ROW, *PLIST_ROW;

/* 以下为vector排序函数 */

bool lessfun_seq(const LIST_ROW &lr1, const LIST_ROW &lr2)

{

return lr1.iSeq < lr2.iSeq;

}

bool greaterfun_seq(const LIST_ROW &lr1, const LIST_ROW &lr2)

{

return lr1.iSeq > lr2.iSeq;

}

bool lessfun_emp(const LIST_ROW &lr1, const LIST_ROW &lr2)

{

int ret = _tcsicmp(lr1.szEmp, lr2.szEmp);

return (ret < 0) ? true : false;

}

bool greaterfun_emp(const LIST_ROW &lr1, const LIST_ROW &lr2)

{

int ret = _tcsicmp(lr1.szEmp, lr2.szEmp);

return (ret > 0) ? true : false;

}

bool lessfun_ip(const LIST_ROW &lr1, const LIST_ROW &lr2)

{

int nIP1 = (int)htonl(inet_addr(lr1.szIP));

int nIP2 = (int)htonl(inet_addr(lr2.szIP));

return nIP1 < nIP2;

}

bool greaterfun_ip(const LIST_ROW &lr1, const LIST_ROW &lr2)

{

int nIP1 = (int)htonl(inet_addr(lr1.szIP));

int nIP2 = (int)htonl(inet_addr(lr2.szIP));

return nIP1 > nIP2;

}

bool lessfun_mac(const LIST_ROW &lr1, const LIST_ROW &lr2)

{

int ret = _tcsicmp(lr1.szMAC, lr2.szMAC);

return (ret < 0) ? true : false;

}

bool greaterfun_mac(const LIST_ROW &lr1, const LIST_ROW &lr2)

{

int ret = _tcsicmp(lr1.szMAC, lr2.szMAC);

return (ret > 0) ? true : false;

}

bool lessfun_type(const LIST_ROW &lr1, const LIST_ROW &lr2)

{

int ret = _tcsicmp(lr1.szType, lr2.szType);

return (ret < 0) ? true : false;

}

bool greaterfun_type(const LIST_ROW &lr1, const LIST_ROW &lr2)

{

int ret = _tcsicmp(lr1.szType, lr2.szType);

return (ret > 0) ? true : false;

}

bool lessfun_beg(const LIST_ROW &lr1, const LIST_ROW &lr2)

{

int ret = _tcsicmp(lr1.szBeg, lr2.szBeg);

return (ret < 0) ? true : false;

}

bool greaterfun_beg(const LIST_ROW &lr1, const LIST_ROW &lr2)

{

int ret = _tcsicmp(lr1.szBeg, lr2.szBeg);

return (ret > 0) ? true : false;

}

bool lessfun_end(const LIST_ROW &lr1, const LIST_ROW &lr2)

{

int ret = _tcsicmp(lr1.szEnd, lr2.szEnd);

return (ret < 0) ? true : false;

}

bool greaterfun_end(const LIST_ROW &lr1, const LIST_ROW &lr2)

{

int ret = _tcsicmp(lr1.szEnd, lr2.szEnd);

return (ret > 0) ? true : false;

}

bool lessfun_mode(const LIST_ROW &lr1, const LIST_ROW &lr2)

{

return lr1.iMode < lr2.iMode;

}

bool greaterfun_mode(const LIST_ROW &lr1, const LIST_ROW &lr2)

{

return lr1.iMode > lr2.iMode;

}

/**

* 函数名称:SortList

* 函数功能:列表排序

* 函数参数:nCol 按第几列

* nType 升序 0 降序 1

*/

void CByPassDlg::SortList(int nCol, int nType)

{

switch (nCol)

{

case 0:// 按序号排序

if (nType == 0)

sort(m_vecListRow.begin(), m_vecListRow.end(), lessfun_seq);

else if (nType == 1)

sort(m_vecListRow.begin(), m_vecListRow.end(), greaterfun_seq);

break;

case 1:// 按用户名排序

if (nType == 0)

sort(m_vecListRow.begin(), m_vecListRow.end(), lessfun_emp);

else if (nType == 1)

sort(m_vecListRow.begin(), m_vecListRow.end(), greaterfun_emp);

break;

case 2:// 按IP排序

if (nType == 0)

sort(m_vecListRow.begin(), m_vecListRow.end(), lessfun_ip);

else if (nType == 1)

sort(m_vecListRow.begin(), m_vecListRow.end(), greaterfun_ip);

break;

case 3:// 按MAC排序

if (nType == 0)

sort(m_vecListRow.begin(), m_vecListRow.end(), lessfun_mac);

else if (nType == 1)

sort(m_vecListRow.begin(), m_vecListRow.end(), greaterfun_mac);

break;

case 4:// 按授信类型排序

if (nType == 0)

sort(m_vecListRow.begin(), m_vecListRow.end(), lessfun_type);

else if (nType == 1)

sort(m_vecListRow.begin(), m_vecListRow.end(), greaterfun_type);

break;

case 5:// 按开始时间排序

if (nType == 0)

sort(m_vecListRow.begin(), m_vecListRow.end(), lessfun_beg);

else if (nType == 1)

sort(m_vecListRow.begin(), m_vecListRow.end(), greaterfun_beg);

break;

case 6:// 按结束时间排序

if (nType == 0)

sort(m_vecListRow.begin(), m_vecListRow.end(), lessfun_end);

else if (nType == 1)

sort(m_vecListRow.begin(), m_vecListRow.end(), greaterfun_end);

break;

case 7:// 按工作模式排序

if (nType == 0)

sort(m_vecListRow.begin(), m_vecListRow.end(), lessfun_mode);

else if (nType == 1)

sort(m_vecListRow.begin(), m_vecListRow.end(), greaterfun_mode);

break;

}

}

void CByPassDlg::OnColumnclickList(NMHDR* pNMHDR, LRESULT* pResult)

{

NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;

// TODO: Add your control notification handler code here

int nSubItem = pNMListView->iSubItem;

// 若原来为升序,则为降序,若原来为降序,则为升序

m_aSortType[nSubItem] = (m_aSortType[nSubItem] == 0) ? 1 : 0;

SortList(nSubItem, m_aSortType[nSubItem]);

m_listCtrl.RedrawItems(0, m_vecListRow.size());

*pResult = 0;

}

注:有时需要用stable_sort.

stable_sort与sort的区别是:保证相等的元素原本相对次序在排序后保持不变。

例如,如下的排序规则就必须使用stable_sort,因为sort会造成排序永远完成不了。

// 顺序: 禁用-下线-启用

bool lessfun_effective(const INNERNET_RECORD &lr1, const INNERNET_RECORD &lr2)

{

if (lr1.nEffective == 0 /*&& (lr1.nEffective == 0 || lr2.nEffective == 1)*/)

{// 两者均是禁用或者前者是禁用、后者是启用,则不必调换

return true;

}

else if (lr1.nEffective == 1 && lr2.nEffective == 0)

{// 前者是启用,后者是禁用,则调换

return false;

}

else

{// 两者都是启用,则根据在线状态决定是否调换

return lr1.nOnline < lr2.nOnline;

}

}

// 顺序:启用-下线-禁用

bool greaterfun_effective(const INNERNET_RECORD &lr1, const INNERNET_RECORD &lr2)

{

if (/*(lr1.nEffective == 1 || lr1.nEffective == 0) && */lr2.nEffective == 0)

{// 前者是启用、后者是禁用或者两者都是禁用,则不必调换

return true;

}

else if (lr1.nEffective == 0 && lr2.nEffective == 1)

{// 前者是禁用,后者是启用,则调换

return false;

}

else

{// 两者都是启用,则根据在线状态决定是否调换

return lr1.nOnline > lr2.nOnline;

}

}

stable_sort(rvecList.begin(), rvecList.end(), lessfun_effective);

stable_sort(rvecList.begin(), rvecList.end(), greaterfun_effective);

即,排序同时参照结构体的两个元素的值。

也可以将排序函数封装到类里面,但是必须定义为static类型的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: