对象更改后对数组\列表进行排序

发布于 2024-09-04 14:38:09 字数 712 浏览 6 评论 0原文

当通用列表的对象属性之一发生更改时,对通用列表进行排序的最佳方法是什么?

我有以下示例来帮助解释需要什么。

public class Sending
{
    public Sending(int id, DateTime dateSent)
    {
        this.Id = id;
        this.DateSent = dateSent;
    }

    public int Id { get; set; }
    public DateTime DateSent { get; set; }
}

public class Operation
{
    public List<Sending> ItemsSent = new List<Sending>();

    public Operation()
    {
        ItemsSent.Add(new Sending(1, new DateTime(2010, 6, 2)));
        ItemsSent.Add(new Sending(2, new DateTime(2010, 6, 3)));

        ItemsSent[1].DateSent = new DateTime(2010, 6, 1);
    }
}

设置 DateSent 属性后,触发列表排序以按日期排序的最佳方法是什么?或者我应该有一种方法来更新属性并执行排序?

What is the best approach for sorting a generic list when one of its objects property is changed?

I have the following example to help explain what is needed.

public class Sending
{
    public Sending(int id, DateTime dateSent)
    {
        this.Id = id;
        this.DateSent = dateSent;
    }

    public int Id { get; set; }
    public DateTime DateSent { get; set; }
}

public class Operation
{
    public List<Sending> ItemsSent = new List<Sending>();

    public Operation()
    {
        ItemsSent.Add(new Sending(1, new DateTime(2010, 6, 2)));
        ItemsSent.Add(new Sending(2, new DateTime(2010, 6, 3)));

        ItemsSent[1].DateSent = new DateTime(2010, 6, 1);
    }
}

What is the best way to trigger a sort on the list to sort by date after the DateSent property is set? Or should I have a method to update the property and perform the sort?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幸福还没到 2024-09-11 14:38:09

您可以在 Sending 上实现 IComparable,并在 ItemsSent 上调用 Sort()。我建议编写一种方法来更新对象并手动更新列表。

public class Sending: IComparable<Sending>
{
  // ...
  public int CompareTo(Sending other)
  {
    return other == null ? 1 : DateSent.CompareTo(other.DateSend);  
  }
}

You could implement IComparable<Sending> on Sending and call Sort() on the ItemsSent. I would suggest to write a method to update an object and update the list manually.

public class Sending: IComparable<Sending>
{
  // ...
  public int CompareTo(Sending other)
  {
    return other == null ? 1 : DateSent.CompareTo(other.DateSend);  
  }
}
醉南桥 2024-09-11 14:38:09

你能做的就是首先实现 INotifyChanged。
然后做一些这样的事情;

public class Sending : INotifyChanged
{
    private int id;
    private DateTime dateSent;

    public Sending(int id, DateTime dateSent)
    {
        this.Id = id;
        this.DateSent = dateSent;
    }

    public int Id { get; set; }
    public DateTime DateSent 
     {
       get
         {
            return this.dateSend;
         }
      set
         {
           this.dateSent = value;
           OnPropertyChangerd("DateSent");
           //CallYou List<Sending> Sort method;
         }
}

因此,每当设置新值时,排序方法都会对列表进行排序。

What you can do is you first implement INotifyChanged.
Then do some thing like this;

public class Sending : INotifyChanged
{
    private int id;
    private DateTime dateSent;

    public Sending(int id, DateTime dateSent)
    {
        this.Id = id;
        this.DateSent = dateSent;
    }

    public int Id { get; set; }
    public DateTime DateSent 
     {
       get
         {
            return this.dateSend;
         }
      set
         {
           this.dateSent = value;
           OnPropertyChangerd("DateSent");
           //CallYou List<Sending> Sort method;
         }
}

So whenever a new value will set the sort method will sort the list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文