如何像在 Windows 资源管理器中一样在 Delphi 中获取排序顺序? [英] How to get the sort order in Delphi as in Windows Explorer?
问题描述
总结:
- 我使用过的术语寻找似乎是自然的排序".
对于操作系统中的行为:
- The terminology that I have been looking for seems to be "natural sort".
For behaviors in operating systems:
- 对于 Windows(版本 >= XP),Windows 资源管理器利用自然排序.
- 对于 Linux 终端:使用ls -v"代替普通的ls"以获得自然排序.
对于在 Delphi 中编程,使用 StrCmpLogicalW Windows API 来获得自然排序.
For programing in Delp use StrCmpLogicalW Windows API to get natural sort.
- (1) 由 Martin Pool 编写的用于自然顺序字符串比较的 Delphi 包装器.
http://irsoft.de/web/strnatcmp-and-natsort-for-delphi - (2) davekeolle 网站上其他语言的字母排序算法代码.
http://www.davekoelle.com/alphanum.html - (3) 其他知识页面:
http://www.codinghorror.com/blog/2007/12/sorting-for-humans-natural-sort-order.html
http://objectmix.com/delphi/722211-natural-sorting-optimizing-working-解决方案.html
http://groups.google.com/group/borland.public.delphi.language.delphi.general/browse_thread/thread/1141d49f8bbba577
http://objectmix.com/delphi/401713-alphanumeric-sort-routine-delphi.html
============================
==========================
以下文件名将在 Windows 资源管理器中排序,如下所示:
The following file names will be ordered in the Windows Explorer as shown below:
test_1_test.txt
test_1_test.txt
test_2_test.txt
test_2_test.txt
test_11_test.txt
test_11_test.txt
test_12_test.txt
test_12_test.txt
test_21_test.txt
test_21_test.txt
test_22_test.txt
test_22_test.txt
例如,如果我将它们放在一个 TStringList 实例中并调用 Sort,则排序顺序如下:
If, for example, I put them in a TStringList instance and call Sort, the sorted order is as below:
test_1_test.txt
test_1_test.txt
test_11_test.txt
test_11_test.txt
test_12_test.txt
test_12_test.txt
test_2_test.txt
test_2_test.txt
test_21_test.txt
test_21_test.txt
test_22_test.txt
test_22_test.txt
为了记录,上述文件名将在 Cygwin 的 rxvt 终端或 CentOS 等 Linux 发行版的 xterm 终端中排序,如下所示:
And for record, the above file names will be ordered in the rxvt terminal of Cygwin or xterm terminal of Linux distributions such as CentOS as shown below:
test_11_test.txt
test_11_test.txt
test_12_test.txt
test_12_test.txt
test_1_test.txt
test_1_test.txt
test_21_test.txt
test_21_test.txt
test_22_test.txt
test_22_test.txt
test_2_test.txt
test_2_test.txt
您能帮忙评论一下如何理解这种排序行为的差异吗?此外,是否可以获得与 Windows 资源管理器中相同的顺序?任何建议表示赞赏!
Could you help to comment on how to understand this difference of sorting behaviors? Furthermore, is it possible to get the same order as in Windows Explorer? Any suggestion is appreciated!
PS:我的 Windows 语言环境设置为中文,但我认为英语语言环境也是如此.
PS: My Windows locale is set to Chinese but I would think the same for English locale.
推荐答案
StrCmpLogicalW 能够处理数字,另一种选择是 比较字符串
StrCmpLogicalW is able to handle numbers, the other alternative is CompareString
这篇关于如何像在 Windows 资源管理器中一样在 Delphi 中获取排序顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!