字符串首字母排序

根据字符串的首字母进行排序。

例如:“我的名字”,提取为“WDMZ”后进行排序。“11我的ab名字”,提取为"11WDabMZ"后进行排序。

void FirstLetter(int nCode, CString& strLetter)
{
	if (nCode >= 1601 && nCode < 1637) strLetter = _T("A");
	if (nCode >= 1637 && nCode < 1833) strLetter = _T("B");
	if (nCode >= 1833 && nCode < 2078) strLetter = _T("C");
	if (nCode >= 2078 && nCode < 2274) strLetter = _T("D");
	if (nCode >= 2274 && nCode < 2302) strLetter = _T("E");
	if (nCode >= 2302 && nCode < 2433) strLetter = _T("F");
	if (nCode >= 2433 && nCode < 2594) strLetter = _T("G");
	if (nCode >= 2594 && nCode < 2787) strLetter = _T("H");
	if (nCode >= 2787 && nCode < 3106) strLetter = _T("J");
	if (nCode >= 3106 && nCode < 3212) strLetter = _T("K");
	if (nCode >= 3212 && nCode < 3472) strLetter = _T("L");
	if (nCode >= 3472 && nCode < 3635) strLetter = _T("M");
	if (nCode >= 3635 && nCode < 3722) strLetter = _T("N");
	if (nCode >= 3722 && nCode < 3730) strLetter = _T("O");
	if (nCode >= 3730 && nCode < 3858) strLetter = _T("P");
	if (nCode >= 3858 && nCode < 4027) strLetter = _T("Q");
	if (nCode >= 4027 && nCode < 4086) strLetter = _T("R");
	if (nCode >= 4086 && nCode < 4390) strLetter = _T("S");
	if (nCode >= 4390 && nCode < 4558) strLetter = _T("T");
	if (nCode >= 4558 && nCode < 4684) strLetter = _T("W");
	if (nCode >= 4684 && nCode < 4925) strLetter = _T("X");
	if (nCode >= 4925 && nCode < 5249) strLetter = _T("Y");
	if (nCode >= 5249 && nCode < 5590) strLetter = _T("Z");
}


int CompareCATALOG_NAME(const void *arg1, const void *arg2)
{
	LPSECURITY_BOOK sb1 = (LPSECURITY_BOOK)arg1;
	LPSECURITY_BOOK sb2 = (LPSECURITY_BOOK)arg2;
	
	CString strTempBook1 = sb1->itemCataName;
	CString	strTempBook2 = sb2->itemCataName;

	int iLength1, iLength2;
	char cName1[1024];
	char cName2[1024];
	BOOL ifChinese1 = FALSE;
	BOOL ifChinese2 = FALSE;

	CString cLetterName1, strTemp1;
	CString cLetterName2, strTemp2;
	unsigned char ucHigh1;
	unsigned char ucLow1;
	DWORD sum1;

	unsigned char ucHigh2;
	unsigned char ucLow2;
	DWORD sum2;

	iLength1 = WideCharToMultiByte(CP_ACP, 0, strTempBook1, -1, NULL, 0, NULL, NULL);
	WideCharToMultiByte(CP_ACP, 0, strTempBook1, -1, cName1, iLength1, NULL, NULL);

	iLength2 = WideCharToMultiByte(CP_ACP, 0, strTempBook2, -1, NULL, 0, NULL, NULL);
	WideCharToMultiByte(CP_ACP, 0, strTempBook2, -1, cName2, iLength2, NULL, NULL);

	for (int i = 0; i < iLength1; i++)
	{
		ucHigh1 = (unsigned char)cName1[i];
		if (ucHigh1 < 128)
		{
			cLetterName1 += cName1[i];
			continue;
		}

		ucLow1 = (unsigned char)cName1[i + 1];
		sum1 = ((ucHigh1 - 0xa0) * 100) + ucLow1 - 0xa0;
		if (ucHigh1 > 128 && ucLow1 > 128)
		{
			FirstLetter(sum1, strTemp1);
			cLetterName1 += strTemp1;
		}
		i++;
	}

	for (int i = 0; i < iLength2; i++)
	{
		int j = 0;
		ucHigh2 = (unsigned char)cName2[i];
		if (ucHigh2 < 128)
		{
			cLetterName2 += cName2[i];
			continue;
		}

		ucLow2 = (unsigned char)cName2[i + 1];
		sum2 = ((ucHigh2 - 0xa0) * 100) + ucLow2 - 0xa0;
		if (ucHigh2 > 128 && ucLow2 > 128)
		{
			FirstLetter(sum2, strTemp2);
			cLetterName2 += strTemp2;
		}
		i++;
	}
	return _tcscmp(cLetterName1.GetBuffer(0), cLetterName2.GetBuffer(0));

}


qsort(m_pCatalogName, m_dwCatalogName, sizeof(SECURITY_BOOK), CompareCATALOG_NAME);




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值