草庐IT

IComparer

全部标签

c# - 构造函数中的 SortedSet<T> 和匿名 IComparer<T> 不起作用

为什么匿名函数可以作为方法的参数,而不是构造函数的参数?如果我创建一个List,它有一个带有以下签名的排序方法:publicvoidSort(IComparercomparer)以下工作的地方:Listlist=newList();list.Sort((a,b)=>a.CompareTo(b));SortedSet有一个具有类似签名的构造函数:publicSortedSet(IComparercomparer)但是在构造函数中使用匿名函数时会失败。以下内容无效:SortedSetset=newSortedSet((a,b)=>a.CompareTo(b));创建排序类按预期正常工作:p

c# - 如何断言两个列表包含 NUnit 中具有相同公共(public)属性的元素?

我想断言两个列表的元素包含我期望的值,例如:varfoundCollection=fooManager.LoadFoo();varexpectedCollection=newList(){newFoo(){Bar="a",Bar2="b"},newFoo(){Bar="c",Bar2="d"}};//assert:IuseAreEquivalentsincetheorderdoesnotmatterCollectionAssert.AreEquivalent(expectedCollection,foundCollection);但是上面的代码将不起作用(我猜是因为.Equals()不

c# - 如何断言两个列表包含 NUnit 中具有相同公共(public)属性的元素?

我想断言两个列表的元素包含我期望的值,例如:varfoundCollection=fooManager.LoadFoo();varexpectedCollection=newList(){newFoo(){Bar="a",Bar2="b"},newFoo(){Bar="c",Bar2="d"}};//assert:IuseAreEquivalentsincetheorderdoesnotmatterCollectionAssert.AreEquivalent(expectedCollection,foundCollection);但是上面的代码将不起作用(我猜是因为.Equals()不

c# - IComparable 和 IComparer 的区别

这个问题在这里已经有了答案:WhentouseIComparableVs.IComparer(8个答案)关闭4年前。IComparable和IComparer接口(interface)有什么区别?是否有必要始终将此接口(interface)与Array.Sort()方法一起使用

c# - IComparable 和 IComparer 的区别

这个问题在这里已经有了答案:WhentouseIComparableVs.IComparer(8个答案)关闭4年前。IComparable和IComparer接口(interface)有什么区别?是否有必要始终将此接口(interface)与Array.Sort()方法一起使用

c# - 使用 IComparer 进行排序

我正在尝试使用IComparer对点列表进行排序。这是IComparer类:publicclassCoordinatesBasedComparer:IComparer{publicintCompare(Objectq,Objectr){Pointa=(p)q;Pointb=(p)r;if((a.x==b.x)&&(a.y==b.y))return0;if((a.x在客户端代码中,我尝试使用此类对点列表p(类型List)进行排序:CoordinatesBasedComparerc=newCoordinatesBasedComparer();Points.Sort(c);代码出错。显然它期

c# - 使用字符串实现自定义 IComparer

例如,我在C#中有一个字符串集合;varexample=newstring[]{"c","b","a","d"};然后我用它来排序,但是我的IComparer方法不起作用,并且看起来无限循环。基本上我需要"b"先来,然后是"c",那么我不关心任何其他人的顺序。这可能使用IComparer和Compare(stringx,stringy)方法?编辑:代码publicintCompare(stringx,stringy){varsOrder=newstring[]{"b","c"};intindex_x=-1;intindex_y=-1;for(inti=0;i=0&&index_y>=0

C# linq sort - 实例化 IComparer 的快速方法

当你使用linq时,你有c.Sort()有没有什么好的内联方式来定义Comparison和/或IComparer类,而无需实际创建单独的类? 最佳答案 这是lambda表达式的用途之一:c.Sort((x,y)=>x.A.CompareTo(y.A)) 关于C#linqsort-实例化IComparer的快速方法,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/1381564/

c# - 将自己的 IComparer<T> 与 Linq OrderBy 结合使用

我有一个通用的List哪里MyClass有一个属性InvoiceNumber其中包含以下值:200906/1200906/2..200906/10200906/11200906/12我的列表绑定(bind)到一个BindingList它支持使用linq进行排序:protectedoverridevoidApplySortCore(PropertyDescriptorproperty,ListSortDirectiondirection){_sortProperty=property;_sortDirection=direction;varitems=this.Items;switch(

c# - 匿名 IComparer 实现

是否可以定义IComparer的匿名实现?我相信Java允许内联定义匿名类-C#允许吗?查看这段代码,我想定义一个自定义IComparer内联publicstaticIOrderedEnumerableOrderBy(thisIEnumerablesource,FunckeySelector,IComparercomparer) 最佳答案 如以下评论之一所示,.Net4.5允许通过Comparer类上的静态方法进行此操作,例如根据类中属性的值比较两个对象:varcomparer=Comparer.Create((k1,k2)=>k1