使用RecursiveDirectoryIterator对目录列表进行排序 [英] Sort directory listing using RecursiveDirectoryIterator

查看:94
本文介绍了使用RecursiveDirectoryIterator对目录列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用RecursiveDirectoryIteratorRecursiveIteratorIterator来使用以下代码构建文件列表树.我需要对列表进行排序-目录然后按字母顺序或仅按字母顺序进行文件.

I'm using RecursiveDirectoryIterator and RecursiveIteratorIterator to build a file listing tree using code like below. I need to the list to be sorted - either directories then files alphabetically, or just alphabetically.

谁能告诉我如何对文件列表进行排序?

Can anyone tell me how to sort the file list?

$dir_iterator = new RecursiveDirectoryIterator($groupDirectory);
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $file) {
    // do stuff with $file
}

推荐答案

使用Iterator本身是不可能的.我在SO上的某个地方看到了Iterator类的扩展,该扩展确实进行了排序,但是模糊地记得遇到了麻烦.

This is not possible using the Iterator itself. I have seen an extension to the Iterator class somewhere on SO that did sorting but remember hazily to have had trouble with it.

也许对此问题的答案有帮助,甚至尽管它们指向的是迭代器?

Maybe the answers to this question help, even though they point away from the Iterator?

更新:

Update: Here is a dupe to your question with some answers - admittedly not many, though!

这篇关于使用RecursiveDirectoryIterator对目录列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆