根据项类中的日期值对ObservableCollection进行排序

时间:2012-07-19 11:03:34

标签: c# silverlight sorting observablecollection

此问题可能看起来像this,但实施方式不同。我只是将这个例子用于我的新实现

我的班级中有以下ObservableCollection,我在Windows Phone 7应用程序中使用该Collection将数据绑定到我的列表框

public ObservableCollection<CustomClass> myList = new ObservableCollection<CustomClass>();

我尝试了以下方式,但排序没有正确发生

我的班级

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类

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

问题不在于逻辑,它与输入日期有关,我查询接下来的15天创建日期而不检查年份 下面的代码正在运行,但需要处理dec / jan月的日子

更新 我的自定义类

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

1 个答案:

答案 0 :(得分:1)

如果你想让这个类自己排序(CreatedDate)

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 Created.CompareTo(other.Created);
    }
}

public class ComparingObservableCollection<T> :  ObservableCollection<T> where T : IComparable<T>
{
    // this function presumes that list is allways sorted and index is not used at all
    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);
            }
        }
    }
}