8

我在我的自定义 IComparer 实现中收到一个空对象,尽管它所应用的集合中没有空条目。我的理解是这可能是由 IComparer 实现中的不一致引起的。我无法在以下代码中发现这可能发生的位置。

作为参考,目的是这些首先按“正确”属性排序,然后如果它们相同,则根据“tiebreakerDelta”属性进行排序,该属性最接近零排序而不会超过。

        public int Compare(IFoolsSortable a, IFoolsSortable b)
    {
        int value1 = a.correct;
        int value2 = b.correct;

        // compare the total correct first
        if (value1 < value2) return 1;
        if (value1 > value2) return -1;

        // total correct is the same, sort on deltas (closest without going over)
        value1 = a.tiebreakerDelta;
        value2 = b.tiebreakerDelta;

        // returning -1 says "put value1 higher in the list than value2" 
        // (higher means closer to the 0 element of the sorted array) 
        if (value1 == 0) return -1; // a zero is higher than anything! 
        if (value2 == 0) return 1; // ditto, for the other arg, if val1 isn't zero 
        if (value1 == value2) return 0; // after that, if they are the same, say so 
        // if both are negative, then the larger one goes higher 
        if (value1 < 0 && value2 < 0) return (value1 > value2) ? -1 : 1;
        // if only one is negative, it goes higher 
        if (value1 < 0) return -1;
        if (value2 < 0) return 1;
        // finally, if both are postitive, lower one goes higher 
        return (value1 > value2) ? 1 : -1;
    }

感谢您提供的任何帮助!

编辑:我确定这不是一个真正的空引用,它是由一些不一致引起的。此外,有时会显示此错误文本以确认 -

Unable to sort because the IComparer.Compare() method returns inconsistent results. Either a value does not compare equal to itself, or one value repeatedly compared to another value yields different results. x: '',  x's type: 'ResultsLineViewModel',

不幸的是,断点对我没有帮助。

编辑:这是一个简短的示例,其中 ResultsLineViewModel 实现了 IFoolsSortable 接口:

List<ResultsLineViewModel> ls = new List<ResultsLineViewModel>();
        ResultsLineViewModel line1 = new ResultsLineViewModel();
        line1.correct = 10;
        line1.tiebreakerDelta = 0;
        ls.Add(line1);

        ResultsLineViewModel line2 = new ResultsLineViewModel();
        line2.correct = 10;
        line2.tiebreakerDelta = 2;
        ls.Add(line2);

        ResultsLineViewModel line3 = new ResultsLineViewModel();
        line3.correct = 10;
        line3.tiebreakerDelta = -3;
        ls.Add(line3);

        ResultsLineViewModel line4 = new ResultsLineViewModel();
        line4.correct = 9;
        line4.tiebreakerDelta = 0;
        ls.Add(line4);

        ls.Sort(new FoolsSort());

正确的排序是: Line1, line3, line2, line4

4

1 回答 1

18

如果a大于bCompare(a,b)应该返回 1 并且Compare(b,a)应该返回 -1。如果你有a.correct = b.correct, and both a.tiebreakerDelta = 0andb.tiebreakerDelta = 0那么这将与Compare方法不一致,因为你想保留操作的顺序。

据我所知,你应该先这样做

if (value1 == value2) return 0; // after that, if they are the same, say so

然后这个:

if (value1 == 0) return -1; // a zero is higher than anything! 
if (value2 == 0) return 1; // ditto, for the other arg, if val1 isn't zero 

另请注意,您的逻辑是相反的,如果第一个大于第二个,您应该返回 1,而不是 -1。检查例如这个链接

于 2012-11-15T22:45:04.247 回答