score:9

Accepted answer

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);

score:1

note that enumeratefiles performs lazy loading and can be more efficient for larger directories, so:

dir.enumeratefiles().orderby(f => f.filename))

score:1

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;

note that directory.enumeratefiles(path) will be more efficient if only the filename is used.

score:2

listpictures = listpictures.orderby(x => x.filename).tolist();

score:2

you can use lambda expression and/or extension methods. for example:

listpictures.orderby(p => p.filename).tolist();

Related Query

More Query from same tag