C#Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
I'm trying to sort a string array of dates using Array.Sort(myArray). It is a one-dimensional array. I am having trouble sorting the dates. Can someone point me in the right direction? It will be greatly appreciated.
Array.Sort(myArray) sorts the array according to month but does not consider the days or the years. What can I do to fix this?
You have a couple of options here. Firstly you can format the dates as strings using YYYYMMDD with no separators. This should ensure that dates are sorted correctly.
The other way to do it is define a new type which stores a DateTime value and implements the IComparable interface. You could then store each date, in this new class, in an object array and the sort function should work correctly.
I tried using:
for (i = 1467; i < 1470; i++)
{
if (i + 1 != 1471)
{
strOne = strSort[i];
strTwo = strSort[i+1];
Comparer.DefaultInvariant.Compare(strOne, strTwo);
}
}
but it does not want to sort the dates correctly. What am I doing wrong? I am new to C#. I know I can do it manually, but I've never actually used C# methods to do this task.
How does the code you've given sort an array? It doesn't store the result anywhere?
As I said, all you have to do is store the strings in your array like YYYYMMDD and then call...
Ok. Nevermind. It is not working...I have to set to storing the dates as integer values in a string array. For example: 1/31/2004 as "1312004" but its not working correctly. Can you please help me some more? Thanks.