Folder names sorting issues after reading from directory
显示 更早的评论
Hi, I used the following code to read all the folders based on the given directory. The issue is that the sorting order shown in figure 1 is not the same as the ones in my explorer window. The folders in the explorer window are sorted based on 'Name'. Based on my file order, the folder in Field 6 should be placed at Field 3 instead.
Question: Is the sorting order performed by Matlab based on some other types??
dinfo = dir('C:\Example');
dinfo(ismember({dinfo.name}, {'.', '..'})) = [];

Figure 1

Figure 2: Folder names sorted based on 'Name'
1 个评论
"The issue is that the sorting order shown in figure 1 is not the same as the ones in my explorer window."
The Windows File Explorer is just some application, it is not the file system nor is it the OS.
"Is the sorting order performed by Matlab based on some other types??"
In fact the File Explorer sorts filenames into an undocumented order (i.e. as you observe it it not the same order as the File System, nor is its order documented anywhere). In contrast the NTFS file system apparently sorts into character code order, and until August 2015 the DIR documentation stated "Results appear in the order returned by the operating system":
As far as I can tell, this is still the case with recent MATLAB releases.
To sort the filenames alphanumerically you can download my FEX submission NATSORTFILES and follow its documentation:
采纳的回答
更多回答(1 个)
I've never noticed a case in which dir returned a list differering in order from that returned by the OS -- what if you look at the result of dinfo BEFORE calling ismember and retrieve those from the original?
[Elided section about removing directory entries from list; forgot these were directories alone, not files OP was looking for...the silly dot and double-dot entries are a pain, certainly, and there's no super-good way to remove them or avoid them. I'm not positive about the [] operation; on reflection it won't change the original order, so it's back to...]
ADDENDUM
As @Image Analyst points out, File Explorer is NOT the OS but a viewing app and can be sorted by any field.
However, dir does not guarantee a specific order and the actual order in absence of a specific switch passed to the DIR command (or the DIRCMD environment variable being set), the returned order is that returned by the OS software API FindFirstFile/FindNextFile dynamic duo. So, that throws the Q? down to what order does that mean? The NTFS file system internally maintains directory entries in a B-tree structure, which for US-English looks approximately alphabetical. So, one normally gets that, but one should never presume they are actually sorted nor write code based on the presumption they are.
If you want them sorted by name and must have as such, then manually sort them in code, the FEX routine that does a natural language sort may be of interest in that regards.
类别
在 帮助中心 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!