Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
595 views
in Technique[技术] by (71.8m points)

sorting - How to get the sort order in Delphi as in Windows Explorer?

Summarization:

  1. The terminology that I have been looking for seems to be "natural sort".
  2. For behaviors in operating systems:

    • For Windows (version >= XP), Windows Explorer utilizes natural sort.
    • For Linux terminals: use "ls -v" instead of plain "ls" to get natural sort.
  3. For programing in Delphi, use StrCmpLogicalW Windows API to get natural sort.

  4. For programing in Delphi & Kylix & Lazarus, use hand-crafted functions to get natural sort:

==========================

The following file names will be ordered in the Windows Explorer as shown below:

test_1_test.txt

test_2_test.txt

test_11_test.txt

test_12_test.txt

test_21_test.txt

test_22_test.txt

If, for example, I put them in a TStringList instance and call Sort, the sorted order is as below:

test_1_test.txt

test_11_test.txt

test_12_test.txt

test_2_test.txt

test_21_test.txt

test_22_test.txt

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_12_test.txt

test_1_test.txt

test_21_test.txt

test_22_test.txt

test_2_test.txt

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: My Windows locale is set to Chinese but I would think the same for English locale.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

StrCmpLogicalW is able to handle numbers, the other alternative is CompareString


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...