简体   繁体   English

错误的List.Sort()行为

[英]Wrong List.Sort() behaviour

I've got a List of Position objects that I retrive from EF DB context 我有一个从EF DB上下文检索的Position对象列表

+--------+---------------------+-------+-------+
|   ID   |      FullDate       | Year  | Month |
+--------+---------------------+-------+-------+
| 21952  | null                | 2015  | 1     |
| 21953  | null                | 2015  | 1     |
| 21954  | null                | 2015  | 1     |
| 21955  | null                | 2015  | 2     |
| 21956  | null                | 2015  | 1     |
| 21957  | null                | 2015  | 2     |
| 21958  | null                | 2015  | 3     |
| 21959  | null                | 2015  | 1     |
| 21960  | null                | 2015  | 1     |
| 21961  | null                | 2015  | 1     |
| 21962  | null                | 2015  | 2     |
| 21963  | null                | 2015  | 2     |
| 21964  | null                | 2015  | 2     |
| 21965  | null                | 2015  | 2     |
| 21966  | 01.02.2015 0:00:00  | null  | null  |
| 21967  | null                | 2015  | 2     |
| 21968  | null                | 2015  | 2     |
| 21969  | null                | 2015  | 2     |
| 21970  | null                | 2015  | 2     |
| 21971  | null                | 2015  | 3     |
| 21972  | null                | 2015  | 3     |
| 21973  | null                | 2015  | 3     |
| 21974  | null                | 2015  | 3     |
| 21975  | null                | 2015  | 3     |
| 21976  | null                | 2015  | 4     |
| 21977  | null                | 2015  | 4     |
| 21978  | null                | 2015  | 4     |
| 21979  | null                | 2015  | 4     |
| 21980  | null                | 2015  | 4     |
| 21981  | null                | 2015  | 5     |
| 21982  | null                | 2015  | 5     |
| 21984  | null                | 2015  | 6     |
| 21983  | null                | 2015  | 5     |
+--------+---------------------+-------+-------+

I've impemented a sorting like so: 我强迫这样的排序:

positions.Sort((x, y) =>
{
    var xDate = getActualDate(x.FullDate, x.Year, x.Month);
    var yDate = getActualDate(y.FullDate, y.Year, y.Month);
    if (xDate > yDate)
    {
        return 1;
    }
    if (xDate == yDate && x.Id> y.Id)
    {
        return 1;                                              
    }                                    
    return -1;
});

Method for getting actual date is 获取实际日期的方法是

private DateTime getActualDate(DateTime? fullDate, int? year, int? month)
{
    return fullDate.HasValue ? fullDate.Value : new DateTime(year.Value, month.Value, DateTime.DaysInMonth(year.Value, month.Value));
}

Every time i try to sort last rows are not changing even if in comparator expression return 1 nothing is changed. 每当我尝试对最后一行进行排序时,即使在比较器表达式返回1时也不会更改。 I've tried to debug Comparator method but get no results everything seems to work without erros exept the result of a sort :( 我试过调试Comparator方法,但是没有任何结果,一切似乎都在没有错误的情况下起作用:(

They can't be actually equal because they'll have different Id's and comparator method will compare Id's in case of equal dates 它们实际上不能相等,因为它们将具有不同的ID,并且比较器方法将在相同日期的情况下比较ID

You still need to code an "equals" case because List.Sort will compare an item to itself at certain points in the process . 您仍然需要编写一个“等于”的案例,因为List.Sort会在流程中的某些点将一个项目List.Sort自身进行比较 Comparing an item to itself should always return 0. 将项目与其自身进行比较应始终返回0。

Luckily, the case is easy enough to inject: 幸运的是,这种情况很容易注入:

positions.Sort((x, y) =>
{
    var xDate = getActualDate(x.FullDate, x.Year, x.Month);
    var yDate = getActualDate(y.FullDate, y.Year, y.Month);
    if (xDate == yDate && x.Id == y.Id)
    {
        return 0;                                              
    }                                    
    if (xDate > yDate)
    {
        return 1;
    }
    if (xDate == yDate && x.Id> y.Id)
    {
        return 1;                                              
    }                                    
    return -1;
});

You can rearrange or logically reduce the comparisons, if you like, but the logic should be the same. 您可以根据需要重新排列或按逻辑减少比较,但是逻辑应相同。

Not sure why the sort isn't working, but this should: 不知道为什么排序不起作用,但这应该:

var result=positions.Select(p=> new { 
  id, 
  date = p.fullDate ?? new DateTime(p.year.Value, p.month.Value, DateTime.DaysInMonth(p.year.Value, p.month.Value))
}).OrderBy(p=>p.date)
  .ThenBy(p=>p.id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM