Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
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
 
Old October 18th, 2004, 05:12 PM
Authorized User
 
Join Date: Aug 2004
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Arsi
Default Sorting Array of date strings

Hello,

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?

*******(*)*******
__________________
*******(*)*******
 
Old October 18th, 2004, 06:00 PM
Kep Kep is offline
Authorized User
 
Join Date: Aug 2003
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

Kep.
 
Old October 19th, 2004, 01:01 PM
Authorized User
 
Join Date: Aug 2004
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Arsi
Default

Thank you for your help! I appreciate it.

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.

*******(*)*******
 
Old October 19th, 2004, 06:03 PM
Kep Kep is offline
Authorized User
 
Join Date: Aug 2003
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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...
Code:
Array.Sort(myArray)
Kep.
 
Old October 22nd, 2004, 04:18 PM
Authorized User
 
Join Date: Aug 2004
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Arsi
Default

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.

*******(*)*******





Similar Threads
Thread Thread Starter Forum Replies Last Post
strings as array indexes cwalton Excel VBA 3 July 30th, 2007 02:50 AM
Array of Strings to dll jjd Excel VBA 2 February 26th, 2006 07:25 AM
Array of Strings jjd Visual C++ 1 June 23rd, 2005 02:56 PM
error when sorting an Array of Array nancy VBScript 2 February 17th, 2005 12:57 PM
sort array of strings error lanita BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 1 December 14th, 2004 08:52 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.