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?
 
sterankinConnect With a Mentor Author 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
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
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
All Courses

From novice to tech pro — start learning today.