新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
今天就跟大家聊聊有关VB.NET中怎么实现按文件名排序,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
为大同等地区用户提供了全套网页设计制作服务,及大同网站建设行业解决方案。主营业务为成都网站设计、成都网站建设、大同网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
输入 : a1,a2,a10,a001
我们知道,如果按照字符串比较,结果应该是 a001,a1,a10,a2,但我们期望的结果应该是a001,a1,a2,a10.
自己写了一个VB.NET文件名排序的算法,请参考,或者有更好的算法,请赐教
/* Return Value Description < 0 arg1 less than arg2 0 arg1 equivalent to arg2> 0 arg1 greater than arg2 */
int compare(const void* arg1,const
void* arg2){
if (NULL==arg1||NULL==arg2)
//address of itemreturn 0;
LPSTR lpText1 = *( TCHAR** )arg1;
//content of itemLPSTR lpText2 = *( TCHAR** )arg2;
//content of itemif (NULL==lpText1||NULL==lpText2)
return 0;
int nText1Len = _tcslen(lpText1);
int nText2Len = _tcslen(lpText2);
int nText1IndexHandled = 0;
int nText2IndexHandled = 0;
int nRet = 0;
for (;;)
{
if (nText1IndexHandled==nText1Len
||nText2IndexHandled==nText2Len)
//don't compare complete since
all are same, "ab","abc"{
TCHAR chOffset1 = nText1IndexHandled
TCHAR chOffset2 = nText2IndexHandled
nRet = (int)((WORD)chOffset1-
(WORD)chOffset2);break;
}
TCHAR ch2 = *(lpText1+nText1IndexHandled);
TCHAR ch3 = *(lpText2+nText2IndexHandled);
if (isdigit(ch2)&&isdigit(ch3))
// if digit, change to number and compare{
TCHAR* lpNum1 = new TCHAR[nText1Len];
TCHAR* lpNum2 = new TCHAR[nText2Len];
if (NULL==lpNum1||NULL==lpNum2)
return 0;
memset(lpNum1,0,nText1Len*sizeof(TCHAR));
memset(lpNum2,0,nText2Len*sizeof(TCHAR));
extractnumber(lpText1,nText1Len,
nText1IndexHandled,lpNum1);extractnumber(lpText2,nText2Len,
nText2IndexHandled,lpNum2);nRet = comparenumber(lpNum1,lpNum2);
delete[] lpNum1;
delete[] lpNum2;
}
else
{
nRet = (int)((WORD)ch2-(WORD)ch3);
nText1IndexHandled++;
nText2IndexHandled++;
}
if (nRet!=0)
break;
}
return nRet;
}
TCHAR* extractnumber(TCHAR* lpBuf,int
nLen,int& nIndexBegin,TCHAR* lpNumber){
if (NULL==lpBuf||NULL==lpNumber)
return lpNumber;
for (int i=nIndexBegin,nIndex=0;i
{
TCHAR ch = *(lpBuf+i);
if (!isdigit(ch))
break;
lpNumber[nIndex++]=ch;
}
return lpNumber;
}
int comparenumber(TCHAR* lpNumber1,
TCHAR* lpNumber2){
if (NULL==lpNumber1||NULL==lpNumber2)
return 0;
int nNum1Len = _tcslen(lpNumber1);
int nNum2Len = _tcslen(lpNumber2);
int nMaxLen = max(nNum1Len,nNum2Len);
TCHAR* lpFormatNum1 = new TCHAR[nMaxLen+1];
TCHAR* lpFormatNum2 = new TCHAR[nMaxLen+1];
if (NULL==lpFormatNum1||NULL==lpFormatNum2)
return 0;
memset(lpFormatNum1,_T('0'),
nMaxLen*sizeof(TCHAR));memset(lpFormatNum2,_T('0'),
nMaxLen*sizeof(TCHAR));lpFormatNum1[nMaxLen]=0;
lpFormatNum2[nMaxLen]=0;
int nPos = 0, nRet = 0;
int nIndex = nMaxLen-1;
for (nPos=nNum1Len-1;nPos>=0;--nPos)
lpFormatNum1[nIndex--]=lpNumber1[nPos];
nIndex = nMaxLen-1;
for (nPos=nNum2Len-1;nPos>=0;--nPos)
lpFormatNum2[nIndex--]=lpNumber2[nPos];
for (nPos=0;nPos
{
nRet = lpFormatNum1[nPos]-lpFormatNum2[nPos];
if (nRet!=0)
break;
}
delete[] lpFormatNum1;
delete[] lpFormatNum2;
return nRet;
}
看完上述内容,你们对VB.NET中怎么实现按文件名排序有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。