Sorting List of Filenames according to Windows

19 ビュー (過去 30 日間)
Simone Croci
Simone Croci 2020 年 9 月 25 日
コメント済み: Walter Roberson 2020 年 10 月 7 日
I have a list of files inside a directory and I would like to sort them with Matlab like Windows sorts the files.
For example, this is a list of files sorted according to Windows:
  • a_.txt
  • a1.txt
  • a2.txt
While this is the list sorted according to the Matlab sort function:
list = {'a_.txt'; 'a1.txt'; 'a2.txt'};
sort(list)
  • a1.txt
  • a2.txt
  • a_.txt
How can I sort the files in Matlab like Windows?
  3 件のコメント
Stephen23
Stephen23 2020 年 9 月 28 日
編集済み: Stephen23 2020 年 9 月 28 日
@Rik: note that the "special symbols" are not sorted into character code order by Windows Explorer (which is presumably what the OP is referring to by the term "Windows"). This is the order that I get using single-character folder names in Win10:
>> +'''-!#$%&(),;@[]^_`{}~+=' % Folder names copied by hand using the order shown by File Explorer.
ans =
39 45 33 35 36 37 38 40 41 44 59 64 91 93 94 95 96 123 125 126 43 61
And this is just a subset of the ASCII characters... what about all of the non-alphabetic Unicode "special symbols", where is this order defined?
Rik
Rik 2020 年 9 月 28 日
I didn't mean to suggest that was a complete description of how Windows Explorer sorts file names. I didn't even mean to suggest it was an incomplete description. I meant it as an example of how you could describe a sort order.

サインインしてコメントする。

回答 (2 件)

Walter Roberson
Walter Roberson 2020 年 9 月 25 日
Windows explicitly does not define a sorting order for files. It leaves it up to the filesystems -- so the sort order for FAT32 might be different than NTFS for example.
I have not come across any formal definition of the sort order for NTFS.
I find a claim that NTFS captures some case-mapping information from the locale of the user at the time the disk is formatted, https://devblogs.microsoft.com/oldnewthing/20050617-10/?p=35293 and uses that, and otherwise sorts by UNICODE code point. MATLAB sorts strictly by UNICODE code point.
The implication is that for the same list of files on two different NTFS drives, the sort order could be different.
Some people do encounter _ sorting before letters, but other people do not encounter that. https://stackoverflow.com/questions/29734737/findnextfile-order-ntfs
  16 件のコメント
Stephen23
Stephen23 2020 年 10 月 2 日
編集済み: Stephen23 2020 年 10 月 3 日
Knowing the character order is still not sufficient: Windows File Explorer also does some kind of number matching as well, and sorts taking into account the number values. But what counts as a number? Decimal comma or point or both? What effect does the locale have on this? Positive/negative sign? What precision? Is there any other special matching of multiple characters? So many questions...

サインインしてコメントする。


U Arda Demiro
U Arda Demiro 2020 年 10 月 7 日
編集済み: U Arda Demiro 2020 年 10 月 7 日
Apparently someone at Matlab fixed this problem, but only in a way that causes other problems. Here is the problem I am having now. Suppose you have four files as follows:
prog1.m
prog1_.m
prog1_5digit.m
prog1_7digit.m
The above order would be how Windows sorts them. Matlab instead currently sorts them as follows:
prog1_.m
prog1_5digit.m
prog1_7digit.m
prog1.m
Fairly annoying.
(Version R2017b, this message: October 7 2020.)
  1 件のコメント
Walter Roberson
Walter Roberson 2020 年 10 月 7 日
I have not seen evidence that MATLAB sorts the names it gets back from the file system calls.
Anyhow, the way to get what you are asking for would appear to be to write some mex to use https://docs.microsoft.com/en-ca/windows/win32/api/shlwapi/nf-shlwapi-strcmplogicalw to sort the names.
However, the links I have presented show that the sort order is not constant and can vary according to the user's settings. The order used by your application can be different.
I would recommend repairing the application that depends upon a fragile sort order.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by