Home > DeveloperSection > Forums > Sort an ArrayList with DateTime using C#
Madam Walker
Madam Walker

Total Post:22

Points:154
Posted on    October-05-2013 7:43 AM

 C# C# 
Ratings:


 1 Reply(s)
 1684  View(s)
Rate this:

 I need to sort the ArrayList based on the StartDate available in the system which is a DateTime variable.

public int id { get; set; }
public string name {get; set;}
public bool isAnctive { get; set; }
public string start { get; set; }
public string end { get; set; }
public DateTime startDate { get; set; }
public DateTime endDate { get; set; }
public string Location { get; set; }
public string Description { get; set; }

Can someone please enlighten me. I tried .Sort() but it just doesn’t do anything (its dump to try sort on ArrayList which has objects I know)

Thanks in advance!



Ida B Wells
Ida B Wells

Total Post:30

Points:210
Posted on    October-05-2013 8:59 AM

Try by creating comparere like as below

public class ComparerDateTime : IComparer
{
    public int Compare(object x, object y)
    {
        MYCLASS X = x as MYCLASS;
        MYCLASS Y = y as MYCLASS;
 
        return X.date.CompareTo(Y.date);
    }
}
 
 MYCLASSList.Sort(new ComparerDateTime ());

Don't want to miss updates? Please click the below button!

Follow MindStick