【问题标题】:Sorting ObservableCollection based on date value in the item class根据项目类中的日期值对 ObservableCollection 进行排序
【发布时间】:2012-07-19 11:03:33
【问题描述】:

这个问题可能看起来像this,但实现方式不同。我仅在我的新实现中使用该示例

我在课堂上关注 ObservableCollection,我在我的 Windows phone 7 应用程序中使用该集合将数据绑定到我的列表框

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

【问题讨论】:

    标签: c# silverlight sorting observablecollection


    【解决方案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);
                }
            }
        }
    }
    

    【讨论】:

    • 我试过了,在我的问题中也提到了它没有正确排序
    • 它没有按日期排序,集合中的项目顺序不正确
    • 可能是因为您使用日期作为字符串?而不是作为日期时间
    • 我在比较var compareDate1 = DateTime.Parse(CreatedDate);之前将其转换为日期时间@
    • 为什么不一直将其存储为 DateTime 呢?您可以有一个属性或构造函数,它接受日期时间作为字符串,然后进行转换。将日期存储为字符串通常很麻烦。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 2017-05-12
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    相关资源
    最近更新 更多