C# 根据item类中的日期值对ObservableCollection进行排序

C# 根据item类中的日期值对ObservableCollection进行排序,c#,silverlight,sorting,observablecollection,C#,Silverlight,Sorting,Observablecollection,这个问题看起来很像,但是实现是不同的。我只是在我的新实现中使用这个示例 我的类中有以下ObservableCollection,我正在Windows phone 7应用程序中使用该集合将数据绑定到我的列表框 public ObservableCollection<CustomClass> myList = new ObservableCollection<CustomClass>(); 公共ObservableCollection myList=新ObservableC

这个问题看起来很像,但是实现是不同的。我只是在我的新实现中使用这个示例

我的类中有以下ObservableCollection,我正在Windows phone 7应用程序中使用该集合将数据绑定到我的列表框

public ObservableCollection<CustomClass> myList = new ObservableCollection<CustomClass>();
公共ObservableCollection myList=新ObservableCollection(); 我尝试了以下方法,但排序不正确

我的班级

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);

    }
 }
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);
            }
        }
  }
}
public类CustomClass:i可比较
{
公共字符串Id{get;set;}
公共字符串名称{get;set;}
公共字符串CreatedDate get{get;set;}
公共整数比较(自定义类其他)
{
var compareDate1=DateTime.Parse(CreatedDate);
var compareDate2=DateTime.Parse(other.CreatedDate);
返回compareDate2.CompareTo(compareDate1);
}
}
子类

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);

    }
 }
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);
            }
        }
  }
}
公共类比较ObservableCollection:ObservableCollection
其中T:i可比较
{
受保护的覆盖无效插入项(int索引,T项)
{
如果(!Items.Contains(item))
{
尝试
{
变量较大=项目。首先(F=>F.CompareTo(项目)>0);
索引=项目。索引(更大);
}
抓住
{
索引=项目数;
}
最后
{
基本插入项(索引,项目);
}
}
}
}
问题不在于逻辑,而在于输入日期,我正在查询未来15天的createddates,而不检查年份 下面的代码正在运行,但需要处理12月/1月的天数

更新 我的自定义类

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);
}
}
public类CustomClass:i可比较
{
公共字符串Id{get;set;}
公共字符串名称{get;set;}
已创建公共日期时间{get;set;}
私有字符串_CreatedDate;
公共字符串创建数据
{
私人获取
{
返回_CreatedDate;
}
设置
{
Created=DateTime.Parse(值);
_CreatedDate=值;
}
}
公共CustomClass(字符串id、字符串名称、创建的字符串)
{
Id=Id;
名称=名称;
CreatedDate=已创建;
}
公共整数比较(自定义类其他)
{
返回CreateDate.Date.DayOfYear.CompareTo(其他.CreateDate.Date.DayOfYear);
}
}

如果您想让此类自行排序(CreatedDate)

public类CustomClass:i可比较
{
公共字符串Id{get;set;}
公共字符串名称{get;set;}
已创建公共日期时间{get;set;}
私有字符串_CreatedDate;
公共字符串创建数据
{
私人获取
{
返回_CreatedDate;
}
设置
{
Created=DateTime.Parse(值);
_CreatedDate=值;
}
}
公共CustomClass(字符串id、字符串名称、创建的字符串)
{
Id=Id;
名称=名称;
CreatedDate=已创建;
}
公共整数比较(自定义类其他)
{
返回已创建。比较(其他已创建);
}
}
公共类ComparingObservableCollection:ObservableCollection,其中T:IComparable
{
//此函数假定列表一直排序,并且根本不使用索引
受保护的覆盖无效插入项(int索引,T项)
{
如果(!Items.Contains(item))
{
尝试
{
变量较大=项目。首先(F=>F.CompareTo(项目)>0);
索引=项目。索引(更大);
}
抓住
{
索引=项目数;
}
最后
{
基本插入项(索引,项目);
}
}
}
}

我试过了,在我的问题中也提到了它没有正确排序它没有按照日期排序,集合中项目的顺序不正确可能是因为您使用了日期作为字符串?在比较
var compareDate1=DateTime.Parse(CreatedDate)之前,我将其转换为DateTime,而不是DateTime为什么不一直将其存储为DateTime?您可以有一个属性或构造函数,它接受datetime作为字符串,然后进行转换。将日期存储为字符串通常非常混乱。