Archivos en el directorio ordenados por nombre de archivo ascendente

I am having a list of files from a directory and I want to sort it out by filename.

Este es el código principal:

var localPath = this.Server.MapPath("~/Content/Img/" + type + "/");
var directory = new DirectoryInfo(localPath);
isDirectory = directory.Exists;

if (isDirectory)
{
    foreach (FileInfo f in directory.GetFiles())
    {
        Picture picture = new Picture();

        picture.ImagePath = path;
        picture.CreationDate = f.CreationTime;
        picture.FileName = f.Name;
        listPictures.Add(picture);
    }
}

here is the class Picture where all the files are stored:

public class Picture
{
    public string ImagePath { get; set; }
    public string FileName { get; set; }
    public DateTime CreationDate { get; set; }
}

How do you do to sort a files list by order of FileName?

preguntado el 28 de agosto de 12 a las 09:08

why downvotes? The question seems legitimate to me -

@SteveB: I think due total simplicity of the question which can be googled or even searched over SO itself, there will be tons of answers! -

Esto ha sido debated on meta. Not sure if this question falls into acceptable or non acceptable question. -

5 Respuestas

Simply change your for loop :

foreach (FileInfo f in directory.GetFiles().OrderBy(fi=>fi.FileName))
{

}

Alternatively, you can rewrite the whole loop using this code :

var sortedFiles = from fi in directory.GetFiles()
                  order by fi.FileName
                  select new Picture { ImagePath = path, CreationDate = f.CreationTime, FileName = f.FileName };

listPictures.AddRange(sortedFiles);

Respondido 28 ago 12, 09:08

listPictures = listPictures.OrderBy(x => x.FileName).ToList();

Respondido 28 ago 12, 09:08

You can use lambda expression and/or extension methods. For example:

listPictures.OrderBy(p => p.FileName).ToList();

Respondido 28 ago 12, 09:08

Tenga en cuenta que enumerar archivos performs lazy loading and can be more efficient for larger directories, so:

dir.EnumerateFiles().OrderBy(f => f.FileName))

Respondido 28 ago 12, 09:08

I may be wrong, but since you're calling OrderBy, I believe Linq will have to process the whole collection before returning the first elements. Therefore, lazy loading won't have any impact. - kevin gosse

Tenga en cuenta que Directory.EnumerateFiles(path) will be more efficient if only the FileName se utiliza. - Tim Schmelter

@KooKiz: Yes, but using EnumerateFiles has the advantage that you can change the query to: dir.EnumerateFiles().Where(f=>f.CreationTime>=DateTime.Now.AddAyears(-1)).OrderBy(f=>f.FileName) without remembering to use now EnumerateFiles en lugar de GetFiles. So why not using it from the beginning? - Tim Schmelter

You can use LINQ from the beginning:

var files = from f in directory.EnumerateFiles()
    let pic = new Picture(){
        ImagePath = path;
        CreationDate = f.CreationTime;
        FileName = f.Name;
    }
    orderby pic.FileName
    select pic;

Tenga en cuenta que Directory.EnumerateFiles(path) will be more efficient if only the FileName se utiliza.

Respondido 28 ago 12, 13:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.