MMDDYYYY filenames sorting using C#.net

RajKumar 101 Reputation points
2021-01-09T03:41:02.46+00:00

Hi All,

I have 5 files like the following abc_12292020.txt, abc_12302020.txt, abc_12312020.txt, abc_01012021.txt, abc_01022021.txt

Now when I am trying to load these files in ascending order its loading in abc_01012021.txt, abc_01022021.txt, abc_12292020.txt, abc_12302020.txt, abc_12312020.txt order.

Here is the C# code I am using

rows.Sort((a, b) => string.Compare(a["Name"].ToString(), b["Name"].ToString()));

Please suggest.

Regards

Raj

SQL Server Integration Services
SQL Server Integration Services
A Microsoft platform for building enterprise-level data integration and data transformations solutions.
2,702 questions
0 comments No comments
{count} votes

Accepted answer
  1. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2021-01-09T10:08:02.143+00:00

    What about

    rows.Sort((a, b) => string.Compare(
                     a.["Name"].substring(a.["Name"].Length-4) + a["Name"].ToString(), 
                     b.["Name"].substring(b.["Name"].Length-4) + b["Name"].ToString());
    

    I don't know why you were using ToString. I left it out since I assume that you already has an array of strings, but if not you will have to add ToString to what I added.

    Warning: I'm a T-SQL guy who only do C# left-handedly.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Jeffrey Williams 1,896 Reputation points
    2021-01-11T21:21:37.267+00:00

    I manage this type of process in SSIS using a script task - that loads an object variable (recordset). I then use a foreach container and loop over the recordset to process the files in order by the date stamp. The object is defined as a dataset (unsorted) - then we use a data view to sort the rows, convert the data view back to a dataset (sorted) and return the sorted dataset in the object.

    In that code - we do the following:

                foreach (FileInfo file in files)
                {
                    // Get File Date from file name
                    string fileName = Path.GetFileNameWithoutExtension(file.Name);
                    DateTime fileDate = DateTime.ParseExact(fileName.Substring(fileName.Length - 8), "yyyyMMdd", provider);
    
                    < other code here >
                }
    

    Our files have a date stamp in the format YYYYMMDD - so when we parse the date that is the format we use. You can change the format to "MMddyyyy" - which will then set an actual date that can be sorted by the date value and not a string. Note: provider is defined as:

                IFormatProvider provider = null;
    
    0 comments No comments

  2. Tom Phillips 17,771 Reputation points
    2021-01-12T19:01:28.803+00:00

    I highly suggest you change your file naming convention to "YYYYMMDD_abc.txt" instead. Then your files will naturally sort without issue.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.