c# and sorting Dates in Arrays

I have a string array of filenames e.g.

String Array[]

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?
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.

string[] s = {

// 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

sterankinAuthor Commented:
Not sure hwo that works - where is x coming from?
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

 return x;


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

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


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.
Gustav BrockCIOCommented:
A solution should work correctly for dates as well.

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

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.

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

From novice to tech pro — start learning today.