zoukankan      html  css  js  c++  java
  • [异常]List<T>.Sort()返回在Compare时出错:IComparer (或其依赖的 IComparable 方法)未返回零 Joe

    今天在实现一个IComparable<Dispatcher>接口时遇到了问题,在执行过程中抛出了如下异常:

    当 Array.Sort 调用 x.CompareTo(x) 时,IComparer (或其依赖的 IComparable 方法)未返回零。x:“”x 的类型:“Dispatcher”IComparer:“”。;StackTrace: 在 System.Collections.Generic.GenericArraySortHelper`1.Sort(T[] keys, Int32 index, Int32 length, IComparer`1 comparer)
    在 System.Array.Sort[T](T[] array, Int32 index, Int32 length, IComparer`1 comparer)
    在 System.Collections.Generic.List`1.Sort(Int32 index, Int32 count, IComparer`1 comparer)
    在 System.Collections.Generic.List`1.Sort()

     

    当时看了一下,也不知道是怎么回事,后来发现应该先确定要比较的两个参数是否是同一个引用,如果是则要返回0。

    步骤如下:

    1、先判断对象为空的情况

    2、都非空,再判断两个参数是否为同一对象引用,若是返回0

    3、最后按对象的属性和相应的参数来对比排序

  • 相关阅读:
    Codeforces Round #375 (Div. 2)
    ACM之路(19)—— 主席树初探
    CodeForces 722D Generating Sets
    CodeForces 721D Maxim and Array
    心情--2014区域赛
    【N-Quens II】cpp
    【N-Queens】cpp
    【Unique Paths II】cpp
    【Unique Paths】cpp
    【Palindrome Partitioning】cpp
  • 原文地址:https://www.cnblogs.com/Smart_Joe/p/2540052.html
Copyright © 2011-2022 走看看