.net 无法比较数组中的两个元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1658661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:31:08  来源:igfitidea点击:

Failed to compare two elements in the array

.netexceptionsorting

提问by Dabblernl

I have a List<T>where Tis a class that exposes a "Username" property. Username is of a custom type that encapsulates a string. I implemented the IComparable<T>interface on this custom type that simply returns

我有一个List<T>whereT是一个公开“用户名”属性的类。用户名是封装字符串的自定义类型。我IComparable<T>在这个简单返回的自定义类型上实现了接口

this.encapsulatedString.CompareTo(other.encapsulatedString)

I defined an ICollectionViewof the List thus:

我这样定义了一个ICollectionView列表:

AllUsers=CollectionViewSource.GetDefaultView(myList);

I added a Sortdescription:

我添加了一个排序描述:

AllUsers.SortDescriptions.Add(new SortDescription("Username",ListSortDirection.Ascending));

On this line the code throws the exception stated in the title. I can sort the list by other means without problem. Where is the exception coming from?

在这一行,代码抛出标题中所述的异常。我可以毫无问题地通过其他方式对列表进行排序。异常来自哪里?

回答by Dabblernl

Stupidstupidstupid: The custom type has to implement IComparableas well as IComparable<T>It seems the SortDescription uses the old fashioned non-generic version of CompareTo

Stupidstupidstupid:自定义类型必须实现IComparable以及IComparable<T>似乎 SortDescription 使用旧式的非通用版本的 CompareTo

I am going to get some much needed sleep...

我要得到一些急需的睡眠......

回答by Mark Lakata

In my case, I added a try/catch block inside the Compare function, and displayed the exception Message to the console. If there is a bug inside your compare function, you will get this secondary exception ("Failed to compare two elements...").

就我而言,我在 Compare 函数中添加了一个 try/catch 块,并将异常消息显示到控制台。如果您的比较函数中存在错误,您将收到此次要异常(“无法比较两个元素...”)。

My problem was specifically with indexing to position 3 of a string that was "" due to another bug.

我的问题特别是索引到由于另一个错误导致的 "" 字符串的位置 3。

回答by MikeKulls

your answer isn't strictly correct from what I can tell. My objects don't implement IComarable or IComparable at all and they still work fine. I am creating a CollectionViewSource and adding sort descriptions just like you and not getting this error. I was getting the error because the property in the sort description was blank. Once I fixed this everything worked fine without the interface. I suspect maybe you had a property incorrect and it drops back to using IComparable if it can't access the property.

据我所知,您的回答并不完全正确。我的对象根本没有实现 IComarable 或 IComparable,它们仍然可以正常工作。我正在创建一个 CollectionViewSource 并像您一样添加排序描述并且没有收到此错误。我收到错误是因为排序描述中的属性为空。一旦我解决了这个问题,没有界面就一切正常。我怀疑您的属性可能不正确,如果无法访问该属性,它会退回使用 IComparable。

回答by thecoop

As you said, you need to implement the non-generic IComparable. You can use the Comparer<T> class if you want to implement this interface in a nice generic way :)

正如您所说,您需要实现非通用的IComparable. 如果你想以一种很好的通用方式实现这个接口,你可以使用 Comparer<T> 类:)

回答by Dan Field

In my case, the property being sorted on was object, and the error was occurring when some of the objects were ints and others were strings.

在我的例子中,被排序的属性是object,并且当一些对象是ints 而其他对象是s时发生错误string

I could've implemented IComparable, but the usage of the class was really more string geared - I was able to change objectto string, and make sure that all setters using numbers called .ToString(), and it was all set from there.

我本可以实现IComparable,但该类的使用确实更适合字符串 - 我能够更改objectstring,并确保所有使用数字的设置器都称为.ToString(),并且所有设置都是从​​那里设置的。