This question may be look like this, but the implementation is different. i am using that example only for my new implementation
I have following ObservableCollection in my class, I am binding data to my listbox using that Collection in my Windows phone 7 application
public ObservableCollection<CustomClass> myList = new ObservableCollection<CustomClass>();
I tried the following way but sorting not happening properly
My Class
public class CustomClass : IComparable<CustomClass>
{
public string Id { get; set; }
public string Name { get; set; }
public string CreatedDate get{ get; set; }
public int CompareTo(CustomClass other)
{
var compareDate1 = DateTime.Parse(CreatedDate);
var compareDate2 = DateTime.Parse(other.CreatedDate);
return compareDate2.CompareTo(compareDate1);
}
}
SUB Class
public class ComparingObservableCollection<T> : ObservableCollection<T>
where T : IComparable<T>
{
protected override void InsertItem(int index, T item)
{
if (!Items.Contains<T>(item))
{
try
{
var bigger = Items.First<T>(F => F.CompareTo(item) > 0);
index = Items.IndexOf(bigger);
}
catch
{
index = Items.Count;
}
finally
{
base.InsertItem(index, item);
}
}
}
}
Problem is not with logic, it is with input dates, i am querying next 15days createddates without checking the year
below code is working but need to handle the days of dec/jan months
UPDATE
My Custom Class
public class CustomClass : IComparable<CustomClass>
{
public string Id { get; set; }
public string Name { get; set; }
public DateTime Created { get; set; }
private string _CreatedDate;
public string CreatedDate
{
private get
{
return _CreatedDate;
}
set
{
Created = DateTime.Parse(value);
_CreatedDate = value;
}
}
public CustomClass(string id, string name, string created)
{
Id = id;
Name = name;
CreatedDate = created;
}
public int CompareTo(CustomClass other)
{
return CreateDate.Date.DayOfYear.CompareTo(other.CreateDate.Date.DayOfYear);
}
}
if you want to have this class sorted by itself (CreatedDate)