快速业务通道

vb.net排序文件名算法

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-04-11

输入 : a1,a2,a10,a001

  我们知道,如果按照字符串比较,结果应该是 a001,a1,a10,a2,但我们期望的结果应该是a001,a1,a2,a10.

  自己写了一个算法,请参考,或者有更好的算法,请赐教

  /*

  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 item

  return 0;

  LPSTR lpText1 = *( TCHAR** )arg1; //content of item

  LPSTR lpText2 = *( TCHAR** )arg2; //content of item

  if (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<nText1Len?lpText1[nText1IndexHandled]:0;

  TCHAR chOffset2 = nText2IndexHandled<nText2Len?lpText2[nText2IndexHandled]:0;

  nRet = (int)((WORD)chOffset1-(WORD)chOffset2);

  break;

  }

  TCHAR ch1 = *(lpText1+nText1IndexHandled);

  TCHAR ch2 = *(lpText2+nText2IndexHandled);

  if (isdigit(ch1)&&isdigit(ch2)) // 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)ch1-(WORD)ch2);

  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<nLen;++i,++nIndexBegin)

  {

  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;--n

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号