您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關VB.NET中怎么按文件名排序,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
VB.NET文件名排序案例:
輸入 : a1,a2,a10,a001 。我們知道,如果按照字符串比較,結果應該是 a001,a1,a10,a2,但我們期望的結果應該是a001,a1,a2,a10.
自己寫了一個VB.NET文件名排序算法
VB.NET code /* 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 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<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;--nPos) lpFormatNum1[nIndex--]=lpNumber1[nPos]; nIndex = nMaxLen-1; for (nPos=nNum2Len-1;nPos>=0;--nPos) lpFormatNum2[nIndex--]=lpNumber2[nPos]; for (nPos=0;nPos<nMaxLen;++nPos) { nRet = lpFormatNum1[nPos]-lpFormatNum2[nPos]; if (nRet!=0) break; } delete[] lpFormatNum1; delete[] lpFormatNum2; return nRet; }
以上就是VB.NET中怎么按文件名排序,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。