Unfortunately, you need to have a comparer of the appropriate type.
You could make a custom IComparer<object>
class that just wrapped the DateTime comparer, but there is no way to do this directly via a cast.
If your collection always contains DateTime objects, then you can just do:
ICollection<DateTime> collection = ...;
collection.Sort(Comparer<DateTime>.Default); // or just collection.Sort()
Edit after reading the comment:
If you're working with an ICollection directly, you may want to use the LINQ option to do:
collection.Cast<DateTime>().OrderBy( date => date );
If you're working with something that implements IList<T>
, (such as List<DateTime>
) you can just call Sort() on the list itself.
Since you're using a non-standard class, you'll need to make your own comparer:
class Comparer : IComparer<object> {
int Compare(object a, object b) {
return DateTime.Compare((DateTime)a, (DateTime)b);
}
}
You can then call:
collection.Sort(new Comparer() );