VB.NET文件名排序轻松掌握

开发 后端
VB.NET文件名排序这一算法在实际应中是一个比较重要的操作步骤。对于初学者来说,需要再学习的过程中对此加强关注。

VB.NET编程语言的应用,帮助开发人员在一定程度上提高了程序开发效率。而且其应用范围比较广泛,使用技巧多样化。比如在对文件的操作上就能够体会到它的功能强大性。首先让我们一起来了解一下VB.NET文件名排序的相关算法。

输入 : a1,a2,a10,a001

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

自己写了一个VB.NET文件名排序的算法,请参考,或者有更好的算法,请赐教

  1. /*  
  2. Return Value Description  
  3. < 0 arg1 less than arg2  
  4. 0 arg1 equivalent to arg2>   
  5. 0 arg1 greater than arg2  
  6. */ 
  1. int compare(const void* arg1,const
     void* arg2)  
  2. {  
  3. if (NULL==arg1||NULL==arg2)
    //address of item  
  4. return 0;  
  5. LPSTR lpText1 = *( TCHAR** )arg1; 
    //content of item  
  6. LPSTR lpText2 = *( TCHAR** )arg2; 
    //content of item  
  7. if (NULL==lpText1||NULL==lpText2)  
  8. return 0;  
  9. int nText1Len = _tcslen(lpText1);  
  10. int nText2Len = _tcslen(lpText2);  
  11. int nText1IndexHandled = 0;  
  12. int nText2IndexHandled = 0;  
  13. int nRet = 0;  
  14. for (;;)  
  15. {  
  16. if (nText1IndexHandled==nText1Len
    ||
    nText2IndexHandled==nText2Len) 
    //don't compare complete since 
    all are same, "ab","abc"  
  17. {  
  18. TCHAR chOffset1 = nText1IndexHandled
    <nText1Len?lpText1[nText1IndexHandled]:0;  
  19. TCHAR chOffset2 = nText2IndexHandled
    <nText2Len?lpText2[nText2IndexHandled]:0;  
  20. nRet = (int)((WORD)chOffset1-
    (WORD)chOffset2);  
  21. break;  
  1. TCHAR ch1 = *(lpText1+nText1IndexHandled);  
  2. TCHAR ch2 = *(lpText2+nText2IndexHandled);  
  3. if (isdigit(ch1)&&isdigit(ch2)) 
    // if digit, change to number and compare  
  4. {  
  5. TCHAR* lpNum1 = new TCHAR[nText1Len];  
  6. TCHAR* lpNum2 = new TCHAR[nText2Len];  
  7. if (NULL==lpNum1||NULL==lpNum2)  
  8. return 0;  
  9. memset(lpNum1,0,nText1Len*sizeof(TCHAR));  
  10. memset(lpNum2,0,nText2Len*sizeof(TCHAR));  
  11. extractnumber(lpText1,nText1Len,
    nText1IndexHandled,lpNum1);  
  12. extractnumber(lpText2,nText2Len,
    nText2IndexHandled,lpNum2);  
  13. nRet = comparenumber(lpNum1,lpNum2);  
  14. delete[] lpNum1;  
  15. delete[] lpNum2;  
  16. }  
  17. else  
  18. {  
  19. nRet = (int)((WORD)ch1-(WORD)ch2);  
  20. nText1IndexHandled++;  
  21. nText2IndexHandled++;  
  22. }  
  23. if (nRet!=0)  
  24. break;  
  25. }  
  26. return nRet;  
  1. TCHAR* extractnumber(TCHAR* lpBuf,int 
    nLen,int& nIndexBegin,TCHAR* lpNumber)  
  2. {  
  3. if (NULL==lpBuf||NULL==lpNumber)  
  4. return lpNumber;  
  5. for (int i=nIndexBegin,nIndex=0;i
    <nLen;++i,++nIndexBegin)  
  6. {  
  7. TCHAR ch = *(lpBuf+i);  
  8. if (!isdigit(ch))  
  9. break;  
  10. lpNumber[nIndex++]=ch;  
  11. }  
  12. return lpNumber;  
  13. }  
  14. int comparenumber(TCHAR* lpNumber1,
    TCHAR* lpNumber2)  
  15. {  
  16. if (NULL==lpNumber1||NULL==lpNumber2)  
  17. return 0;  
  18. int nNum1Len = _tcslen(lpNumber1);  
  19. int nNum2Len = _tcslen(lpNumber2);  
  20. int nMaxLen = max(nNum1Len,nNum2Len);  
  21. TCHAR* lpFormatNum1 = new TCHAR[nMaxLen+1];  
  22. TCHAR* lpFormatNum2 = new TCHAR[nMaxLen+1];  
  23. if (NULL==lpFormatNum1||NULL==lpFormatNum2)  
  24. return 0;  
  25. memset(lpFormatNum1,_T('0'),
    nMaxLen*sizeof(TCHAR));  
  26. memset(lpFormatNum2,_T('0'),
    nMaxLen*sizeof(TCHAR));  
  27. lpFormatNum1[nMaxLen]=0;  
  28. lpFormatNum2[nMaxLen]=0;  
  29. int nPos = 0nRet = 0;  
  30. int nIndex = nMaxLen-1;  
  31. for (nPos=nNum1Len-1;nPos>=0;--nPos)  
  32. lpFormatNum1[nIndex--]=lpNumber1[nPos];  
  33. nIndex = nMaxLen-1;  
  34. for (nPos=nNum2Len-1;nPos>=0;--nPos)  
  35. lpFormatNum2[nIndex--]=lpNumber2[nPos];  
  36. for (nPos=0;nPos<nMaxLen;++nPos)  
  37. {  
  38. nRet = lpFormatNum1[nPos]-lpFormatNum2[nPos];  
  39. if (nRet!=0)  
  40. break;  
  41. }  
  42. delete[] lpFormatNum1;  
  43. delete[] lpFormatNum2;  
  44. return nRet;  

VB.NET文件名排序的相关算法就为大家介绍到这里。

【编辑推荐】

  1. VB.NET IEnumerator接口操作代码解读
  2. VB.NET历史菜单轻松上手
  3. VB.NET对话框基本思想及应用技巧分享
  4. VB.NET通信程序基础概念详解
  5. VB.NET动态生成代码相关经验分享
责任编辑:曹凯 来源: 中国IT实验室
相关推荐

2009-10-27 10:58:00

VB.NET文件名排序

2009-10-29 15:02:04

VB.NET文件排序

2009-11-02 11:13:06

VB.NET读写文件

2010-01-13 17:47:59

VB.NET拖放

2010-01-14 13:59:01

2010-01-18 19:36:52

VB.NET调整控件

2010-01-14 11:00:48

VB.NET文件合并

2009-10-29 09:06:26

VB.NET Web

2010-01-13 18:28:21

VB.NET历史菜单

2010-01-11 15:12:30

VB.NET特殊窗体

2009-10-30 13:31:06

VB.NET名空间

2010-01-12 10:19:02

VB.NET操作GDI

2010-01-08 18:16:52

VB.NET变量

2009-10-29 14:16:32

VB.NET读写文本文

2009-11-03 09:37:33

VB.NET重载

2009-10-29 13:38:05

VB.NET Shar

2010-01-11 14:28:14

VB.NET操作Exc

2009-10-29 15:50:49

VB.NET Exce

2010-01-07 11:07:20

VB.NET读取INI

2009-10-30 14:45:42

Flash控制VB.N
点赞
收藏

51CTO技术栈公众号