如何根据以下文件集中的时间戳对文件进行排序?

时间:2010-08-10 09:22:33

标签: c# python ruby-on-rails

File dir = new File("."); 
FileFilter fileFilter = new WildcardFileFilter("sample*.java"); 
File[] files = dir.listFiles(fileFilter); 
for (int i = 0; i < files.length; i++) { 
   System.out.println(files[i]); 
} 

例如:

如果我在目录中显示以下文件:

 FILE NAME                     DATE CREATED/MODIFIED

properties.txt                    10/08/2010 06:19
sublime.dll                       10/08/2010 08:01
css_stlyle.css                    10/08/2010 10:00
BMW_tags.php                      10/08/2010 19:03
cars.properties                   10/08/2010 04:37

3 个答案:

答案 0 :(得分:1)

这个C#linq方法怎么样:

var query = Directory.GetFiles("D:\\", "*.txt", SearchOption.AllDirectories)
                     .Select(name => new FileInfo(name));

var orderedList = query.OrderBy(fileInfo => fileInfo.CreationTime).ToList();

答案 1 :(得分:0)

在Python中,如果我理解正确的话:

import os
sorted( os.listdir( "." ), key = lambda file: os.stat( file )[ 8 ] )

答案 2 :(得分:0)

因为您的示例位于java中,请使用Comparator

File dir = new File("."); 
        File[] files = dir.listFiles(); 

        Arrays.sort(files, new Comparator<File>() {

            public int compare(File arg0, File arg1) {
                return (arg0.lastModified() < arg1.lastModified())? -1 : 1;
            }
        });
        for (int i = 0; i < files.length; i++) { 
           System.out.println(files[i] + " : " + files[i].lastModified()); 
        }