Hi I have something like the code below, I want to sort/order the files in the folder lets say by name of the file in asc or dsc order, before binding to the gridview:
string[] filePaths = Directory.GetFiles(Server.MapPath("~/Uploads/")); List<ListItem> files = new List<ListItem>(); foreach (string filePath in filePaths) { files.Add(new ListItem(Path.GetFileName(filePath), filePath)); } GridView1.DataSource = files; GridView1.DataBind();
You can simply order by after foreach loop
files = files.OrderBy(x => x.Text).ToList();
For Order by descending
files = files.OrderByDescending(x => x.Text).ToList();
2.1m questions
2.1m answers
60 comments
57.0k users