c# and sorting Dates in Arrays

I have a string array of filenames e.g.

String Array[]
File_3_19_2010_10_13_14_AM_1.txt
File_3_19_2010_11_10_09_AM_1.txt
File_3_19_2010_11_44_19_AM_1.txt
File_3_19_2010_13_44_19_PM_1.txt
File_3_19_2010_14_28_23_PM_1.txt
File_3_19_2010_08_32_54_AM_1.txt
File_3_19_2010_09_01_05_AM_1.txt
 

I have managed to loop through each string in this string array and parse out the datetimes and put them in ANOTHER array.  This other array is a DateTime[] array, and contains the datetimes from each file name.

i.e. DateTime Array[]
3/19/2010 10:13:14 AM
3/19/2010 11:10:09 AM
3/19/2010 11:44:19 AM
3/19/2010 13:44:19 PM
3/19/2010 14:28:23 PM
3/19/2010 08:32:54 AM
3/19/2010 09:01:05 AM

So I have 2 arrays, one is a string array and one is a datetime array - and they match up.

I need to sort the datetime array, with lowest date first - but I need to APPLY the sort to the string array.  So I need the string array sorted the same as if it were the DateTime array[].

i.e. I need to order the string array file names by date.

How can this be done simply?
sterankinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Richard LeeSoftware EnthusiastCommented:
As the time is in 24hr format you can simply remove the character after the time without the AM/PM and sort as normal.

DaTribe
string[] s = {
             	"File_3_19_2010_10_13_14_AM_1.txt",
             	"File_3_19_2010_11_10_09_AM_1.txt",
             	"File_3_19_2010_11_44_19_AM_1.txt",
             	"File_3_19_2010_13_44_19_PM_1.txt",
             	"File_3_19_2010_14_28_23_PM_1.txt",
             	"File_3_19_2010_08_32_54_AM_1.txt",
             	"File_3_19_2010_09_01_05_AM_1.txt"
             };

// Since time is specified in 24hr format
// simple remove the AM/PM and sort as normal
var result = s
	.Select(x => x.Substring(0, x.LastIndexOf('_') - 3))
	.OrderBy(x => x);

Open in new window

0
sterankinAuthor Commented:
Not sure hwo that works - where is x coming from?
0
Richard LeeSoftware EnthusiastCommented:
I'm using Linq. In this case I'm using lambda expressions and that format x => x is a short hand way of writing

delegate(x)
{
 return x;
}

http://msdn.microsoft.com/en-us/netframework/aa904594.aspx

The result is an array of strings (string[]) therefore you can work with it as normal

DaTribe
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

sterankinAuthor Commented:
I got it using a bubble sort:

 int x = listDates.Length;
 int c;
  int j;
  DateTime temp; //for the datetime array
  string temp2; //for the String array which is what we want sorted

  for (c = (x - 1); c >= 0; c--)
  {
      for (j = 1; j <= c; j++)
      {
          if (listDates[j - 1] > listDates[j])
          {
//sorts both arrays based on the date array
              temp = listDates[j - 1];
              temp2 = aCopy[j - 1];
              listDates[j - 1] = listDates[j];
              aCopy[j - 1] = aCopy[j];
              listDates[j] = temp;
              aCopy[j] = temp2;
          }
      }
  }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Richard LeeSoftware EnthusiastCommented:
Points must be awarded as a working solution was provided and all posed questions were answered. Yes the author found another solution himself however there was nothing wrong with the solution provided and it works.
0
Gustav BrockCIOCommented:
A solution should work correctly for dates as well.

/gustav
0
sterankinAuthor Commented:
DaTribes code did not compile in my project and I still cannot get it working.  I am using .NET 2005
0
Gustav BrockCIOCommented:
This could be done in a lot of ways but if your method is working I would stick with that.

/gustav
0
Richard LeeSoftware EnthusiastCommented:
Ok mate. I will concede as I gave you c# 3.0 code. Next time specify which .NET Framework you are using.

DaTribe
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.