例如,文件夹下包含这些文件名:
1.jpg
2.jpg
3.jpg
......
10.jpg
11.jpg
通用的排序方式按增序进行排序会得到这样的排序结果
1.jpg
10.jpg
11.jpg
2.jpg
3.jpg
......
原因在于字符串比较过程根据字符串中的字符进行比较,没有考虑到字符串中数字的逻辑意义。
Windows中有一个API专门用来解决字符串逻辑比较任务:StrCmpLogicalW
用法如下:
int StrCmpLogicalW(
LPCWSTR psz1,
LPCWSTR psz2
);
参数:
psz1
[in] A pointer to the first null-terminated string to be compared
psz2
[in] A pointer to the second null-terminated string to be compared
返回值:
Returns zero if the strings are identical
Returns 1 if the string pointed to by psz1 has a greater value than that pointed to by psz2
Returns -1 if the string pointed to by psz1 has a lesser value than that pointed to by psz2
相关链接:https://blog.csdn.net/yanghao58686763/article/details/6599157