0

I'm trying to sort the data in lambda expression in the following case.

if (Directory.Exists(Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["ErrorLogPath"].ToString())))
{
    string path = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["ErrorLogPath"].ToString());
    // a.Select(p => Path.GetFileNameWithoutExtension(p));
    var a = Directory.GetFiles(path);
    if (a != null)
    {
        Session["gvData"] = a.ToList();
        BindDataToGrid();
    }
}

In var a i get list of complete path of files like

c:\\logfiles\\01022012.txt. 

How can I get var a sorted on basis of 01022012 I tried

var a = Directory.GetFiles(path).OrderBy(p=>Path.GetFileNameWithoutExtension(p));

but not working. Is there any thing am I doing wrong?

Getting result as

"C:\\LogFiles\\01112012.txt" 
"C:\\LogFiles\\08102012.txt" 
"C:\\LogFiles\\14092012.txt" 
"C:\\LogFiles\\15102012.txt" 
"C:\\LogFiles\\17102012.txt" 
"C:\\LogFiles\\19092012.txt" 

Expected is

"C:\\LogFiles\\14092012.txt" 
"C:\\LogFiles\\19092012.txt" 
"C:\\LogFiles\\08102012.txt" 
"C:\\LogFiles\\15102012.txt" 
"C:\\LogFiles\\17102012.txt" 
"C:\\LogFiles\\01112012.txt" 
6
  • It's not clear where LINQ to Entities fits in here - you're using Directory.GetFiles(), so that's an in-memory data source, so you're using LINQ to Objects. Next, please clarify what you mean by "not working" - what exactly are you seeing?
    – Jon Skeet
    Nov 3, 2012 at 9:21
  • not working means i get same result as not applying .OrderBy(p=>Path.GetFileNameWithoutExtension(p)); i get data but not sorted one. Nov 3, 2012 at 9:26
  • I suspect your diagnostics are incorrect, or the filenames were already sorted. Please provide a short but complete example, the expected result and the actual result.
    – Jon Skeet
    Nov 3, 2012 at 9:34
  • Getting result as "C:\\LogFiles\\01112012.txt" "C:\\LogFiles\\08102012.txt" "C:\\LogFiles\\14092012.txt" "C:\\LogFiles\\15102012.txt" "C:\\LogFiles\\17102012.txt" "C:\\LogFiles\\19092012.txt" Expected is "C:\\LogFiles\\14092012.txt" "C:\\LogFiles\\19092012.txt" "C:\\LogFiles\\08102012.txt" "C:\\LogFiles\\15102012.txt" "C:\\LogFiles\\17102012.txt" "C:\\LogFiles\\01112012.txt" Nov 3, 2012 at 9:42
  • sorry lazyberezovsky for not marking answer as marked. yes some of the answers helped me. i marked them. Nov 3, 2012 at 10:06

1 Answer 1

2
var a = Directory.GetFiles(path)
                 .OrderBy(p => Regex.Replace(p,@"^.*\\(\d\d)(\d\d)(\d\d\d\d).*$","$3$2$1"))

The order worked but was an alphabetical order, whereas you want a chronological order. The trick is to turn ddMMyyyy into yyyyMMdd (thanks to the regex) and then then alphabetical and chronological orders become identical.

2
  • thanks Ssithra. it worked. i am understanding regular expression. its sorting in ascending order. what changes i need to do to make it sorting descending ? Nov 3, 2012 at 10:28
  • 1
    @SarangAmrutkar use .OrderByDescending Nov 3, 2012 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.