Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3458346
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:58:17+00:00 2026-05-18T09:58:17+00:00

I have a list of files I would like to sort based on the

  • 0

I have a list of files I would like to sort based on the date. The caveat is that each file in the list contains the date as part of it’s file name, I want to sort the files based on this date. The reason for this is because the date in the file name string correlates with the content that is in the file, i.e. The actual date property of each file, date created, modified, accessed, etc, is subject to change whenever the file is moved or modified and so I cannot rely on it for my purposes. I’m creating a custom comparer to use in the list sorting method, but I wanted to see if anyone out there has any other better or unique approaches to this. Thanks in advance.

UPDATE:

In answer to Saeed’s comment, the file name follows this format:

{Test Name}_{YYYYMMDD}_{HHmmSS}.txt

Where the YYYYMMDD and HHmmSS are the date and time, respectively.

UPDATE 2:

Ok, I got a comparer written up that seems to do the job just fine, here it is if it helps anyone else; it wouldn’t take much effort to alter it to search for any other elements in the file name, just need to change the regular expression. Thanks everyone for the suggestions, constructive criticism is always welcome.

public class TDRDateTimeComparer : IComparer<FileInfo>
{
    public int Compare(FileInfo x, FileInfo y)
    {
       //Return if both strings are null or empty
        if (string.IsNullOrEmpty(x.Name) && string.IsNullOrEmpty(y.Name))
        {
            return 0;
        }
        Regex rDateTime = new Regex(@"(19|20|21)\d\d(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])_([0-1]\d|2[0-3])([0-5]\d)([0-5]\d)");

        /* NOTE: File names will already have been validated at this point, so no need to validate the date/time again.*/
        string date1 = rDateTime.Match(x.Name).Value, date2 = rDateTime.Match(y.Name).Value;
        DateTime d1 = DateTime.ParseExact(date1, "yyyyMMdd_HHmmss", null), d2 = DateTime.ParseExact(date2, "yyyyMMdd_HHmmss", null);

        return d1.CompareTo(d2);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T09:58:18+00:00Added an answer on May 18, 2026 at 9:58 am

    I’d say a custom comparer is the way to go. Just use regex to extract the date parts, then create a DateTime out of it and compare those.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of files that I would like analyze. They are all
I have a long list of files and file extensions which I would like
I have a list of files on a server and would like to load
I have a directory with several .html files. Would like to read the list
Let's imagine that I have list of files at host1 find /path/to -name *.jpg
I have 3 text files each of which contains a list of 20 items.
I have a list of numbers which total 540000. I would like to sort
I would like to sort a list of 1500 files in XSLT 1. The
I have an input file that I want to sort based on timestamp which
I have a list of files like this: wgEncodeCaltechRnaSeqGm12878R1x75dFastqRep1.fastq.trim.tags.sam wgEncodeCaltechRnaSeqGm12878R1x75dFastqRep2.fastq.trim.tags.sam wgEncodeCshlLongRnaSeqGm12878CellPapFastqRd1Rep1.fastq.trim00.tags.sam wgEncodeCshlLongRnaSeqGm12878CellPapFastqRd1Rep1.fastq.trim01.tags.sam wgEncodeCshlLongRnaSeqGm12878CellPapFastqRd1Rep1.fastq.trim02.tags.sam wgEncodeCshlLongRnaSeqGm12878CellPapFastqRd1Rep2.fastq.trim00.tags.sam

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.