Results 1 to 2 of 2

Thread: [2.0] What is the best way to arrange dates?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Location
    I'm right here!
    Posts
    849

    Question [2.0] What is the best way to arrange dates?

    Hello!

    I have a few instances of a class that I have created.
    The class has a DateTime property.

    In my list of instances, I want to arrange all classes in chronological order, according to the DateTime property, from the newest to the oldest.
    How can I do that?

    Thanks!
    Dekel C.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] What is the best way to arrange dates?

    If this is something you might need to do in more than one place then you should define a class that implements the IComparer interface and compares two instances of your type by that property. There are generic and standard versions of the IComparer interface but it should be enough to just implement the generic version.
    C# Code:
    1. public class SomeType
    2. {
    3.     private DateTime _date;
    4.  
    5.     public DateTime Date
    6.     {
    7.         get
    8.         {
    9.             return _date;
    10.         }
    11.         set
    12.         {
    13.             _date = value;
    14.         }
    15.     }  
    16. }
    C# Code:
    1. public class SomeTypeComparer : IComparer<SomeType>
    2. {
    3.     public int Compare(SomeType x, SomeType y)
    4.     {
    5.         return DateTime.Compare(x.Date, y.Date);
    6.     }
    7. }
    Now you can sort a List or array of your instances of your type by passing an instance of your IComparer to the Sort method:
    C# Code:
    1. List<SomeType> list = new List<SomeType>();
    2.  
    3. // Add items here.
    4.  
    5. // Sort by Date property.
    6. list.Sort(new SomeTypeComparer());
    If you only need to do it in one place then you should use a Comparison delegate instead. In that case you declare a method that has the same signature as the Comparison delegate:
    C# Code:
    1. private int CompareSomeTypes(SomeType x, SomeType y)
    2. {
    3.     return DateTime.Compare(x.Date, y.Date);
    4. }
    Note that it is basically the same as the Compare method of the IComparer. To sort your array or list you now create a Comparison delegate for that method:
    C# Code:
    1. List<SomeType> list = new List<SomeType>();
    2.  
    3. // Add items here.
    4.  
    5. // Sort by Date property.
    6. list.Sort(new Comparison<SomeType>(CompareSomeTypes));
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width