无法创建比较器

发布于 2025-01-07 10:52:08 字数 884 浏览 0 评论 0原文

我有一个类

public class PAUserAllowedTimesModel
{
    public List<AllowedTime> Times { get; set; }
    public List<AllowedTime> BusyTimes { get; set; }
    public DateTime SelectedDate { get; set; }
    public int DateID { get; set; }
}

,我有该类的对象列表:

List<PAUserAllowedTimesModel> model = ...

我想按 SelectedDate 对该集合进行排序。我尝试:

public class PAUserAllowedTimesModelComparer : IComparer<ITW2012Mobile.ViewModels.PAUserAllowedTimesModel>
{
    public int Compare(ViewModels.PAUserAllowedTimesModel x, ViewModels.PAUserAllowedTimesModel y)
    {
        if (x.SelectedDate > y.SelectedDate)
            return 0;
        else
            return 1;
    }
}

然后

model.Sort(new PAUserAllowedTimesModelComparer());

但它只是混合元素,而不是排序。怎么了?

I have a class

public class PAUserAllowedTimesModel
{
    public List<AllowedTime> Times { get; set; }
    public List<AllowedTime> BusyTimes { get; set; }
    public DateTime SelectedDate { get; set; }
    public int DateID { get; set; }
}

I have a list of object of this class:

List<PAUserAllowedTimesModel> model = ...

I want to sort this collection by SelectedDate. I try:

public class PAUserAllowedTimesModelComparer : IComparer<ITW2012Mobile.ViewModels.PAUserAllowedTimesModel>
{
    public int Compare(ViewModels.PAUserAllowedTimesModel x, ViewModels.PAUserAllowedTimesModel y)
    {
        if (x.SelectedDate > y.SelectedDate)
            return 0;
        else
            return 1;
    }
}

and then

model.Sort(new PAUserAllowedTimesModelComparer());

but it just mix elements, not sort. What is wrong?

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

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

发布评论

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

评论(2

∞觅青森が 2025-01-14 10:52:08

您的比较器将永远返回-1,因此它违反了Compare约定...

幸运的是,无论如何您都可以使其变得更简单:

public int Compare(ViewModels.PAUserAllowedTimesModel x, 
                   ViewModels.PAUserAllowedTimesModel y)
{
    // Possibly reverse this, depending on what you're trying to do
    return x.SelectedDate.CompareTo(y.SelectedDate);
}

或者使用LINQ:

model = model.OrderBy(x => x.SelectedDate).ToList();

请注意,这不会进行就地排序,与List.Sort不同。

Your comparer will never return -1, so it's violating the Compare contract...

Fortunately you can make it much simpler anyway:

public int Compare(ViewModels.PAUserAllowedTimesModel x, 
                   ViewModels.PAUserAllowedTimesModel y)
{
    // Possibly reverse this, depending on what you're trying to do
    return x.SelectedDate.CompareTo(y.SelectedDate);
}

Or using LINQ:

model = model.OrderBy(x => x.SelectedDate).ToList();

Note that this doesn't do an in-place sort, unlike List<T>.Sort.

老娘不死你永远是小三 2025-01-14 10:52:08

您的 IComparer 实现是错误的。如果元素相等则需要返回0,如果x > 则返回1。如果 y > 则为 y,并且 -1 x 或反之亦然,具体取决于您要按降序还是升序排序。

Your implementation of IComparer is wrong. You need to return 0 if the elements are equal, 1 if x > y and -1 if y > x or vice versa, depending on whether you want to sort descending or ascending.

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