Forum Moderators: open

Message Too Old, No Replies

sort by strings of MMDDYY format

         

IntegrityWebDev

7:30 pm on Oct 5, 2010 (gmt 0)

10+ Year Member



I have a directory with a lot of sub-directories in it. I've filtered out all other directories to where I have an ArrayList of only directories names in this format:
MMDDYY

I need to get the 3 most recent dates, newest to oldest. Any thoughts on how to do that (in C#)?

Thanks!
Chris

dstiles

7:57 pm on Oct 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



There is only one valid date format (assuming it's not a DATE field in a database) and that is: YYMMDD (then time if required). All else cause problems. It's easy enough to convert inside the software if necessary (eg to display dayname, daynumber, monthname, year).

Otherwise, assuming the directory Creation date does not change, read the File Dates and go by those.

LifeinAsia

8:06 pm on Oct 5, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



If your subdirectories were named YYMMDD instead, your work would pretty much be done for you (assuming that the years are all in the same century) as they would automatically order naturally.

You can convert the names to YYMMDD, sort, then convert back.

No idea how to do it in C#.

IntegrityWebDev

8:32 pm on Oct 5, 2010 (gmt 0)

10+ Year Member



I didn't think about using the directory creation date...that might work for me. I will see what I can do with that.

g1smd

9:57 pm on Oct 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



YYMMDD

Go for YYYYMMDD if you can.

See also ISO 8601, RFC 3339, ANSI X3.30, NIST FIPS 4-2, etc.

tangor

1:22 am on Oct 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Second g1smd YYYYMMDD solves a multitude of problems.

IntegrityWebDev

12:45 pm on Oct 6, 2010 (gmt 0)

10+ Year Member



The directories are already in place and they have wide reaching web-links so I can't change the format of the directory names. Any other thoughts?

Fotiman

2:04 pm on Oct 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The Sort method of ArrayList can take a custom Comparer, so you could write a Comparer that swaps characters 1&2 with 3&4 before doing the comparison. It would probably look something like this:

public class MMDDYYComparer : IComparer {
int IComparer.Compare( Object x, Object y ) {
string X = (string)x;
string Y = (string)Y;
X = (X.subString(2,2) + X.subString(0,2) + X.subString(4));
Y = (Y.subString(2,2) + Y.subString(0,2) + Y.subString(4));
return( (new CaseInsensitiveComparer()).Compare( X, Y ) );
}
}
IComparer myComparer = new MMDDYYComparer();
myArrayList.Sort( myComparer );

IntegrityWebDev

3:08 pm on Oct 6, 2010 (gmt 0)

10+ Year Member



Using Fotiman's thoughts on Substring I got it working. I used a few ArrayLists to do it. i know ArrayList isn't the way you should go but in the end it did work for my job and for the server I'm on...here's what I did:
  1. Read all directories into an ArrayList
  2. Used RegEx to remove all directories I didn't need, leaving only those in a MMDDYY format.
  3. Used Substring to create a new Array list in the in the YYMMDD format
  4. Sorted then reversed that list
  5. Used that to take out all but the most recent 5 (with a for loop)
  6. Used substring to create a new ArrayList where the directory name is in the original MMDDYY order.


Definitely not the most tidy or streamlined method but it did work and will do the trick for what I need. Thanks all!

-Chris

Fotiman

3:28 pm on Oct 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm not sure why you would go to the trouble of creating another ArrayList in the YYMMDD format when you could simply use a Comparer to do the work for you. Also, if you wanted them in reverse order, then in my example above you could have changed it do this:

return( (new CaseInsensitiveComparer()).Compare( Y, X ) );

But glad you found a solution.