icomparer

Pass a lambda expression in place of IComparer or IEqualityComparer or any single-method interface?

不想你离开。 提交于 2019-11-27 11:52:48
问题 I happened to have seen some code where this guy passed a lambda expression to a ArrayList.Sort(IComparer here) or a IEnumerable.SequenceEqual(IEnumerable list, IEqualityComparer here) where an IComparer or an IEqualityComparer was expected. I can't be sure if I saw it though, or I am just dreaming. And I can't seem to find an extension on any of these collections that accepts a Func<> or a delegate in their method signatures. Is there such an overload/extension method? Or, if not, is it

Implementing custom IComparer with string

时光总嘲笑我的痴心妄想 提交于 2019-11-27 09:25:06
I have a collection of strings in c#, for example; var example = new string[]{"c", "b", "a", "d"}; I then with to sort this, but my IComparer method is not working, and looping infinitely by the seems of things. Basically I need "b" to come first, followed by "c" , then I dont care about the order of any of the others. Is this possible using I Comparer<string> and the Compare(string x, string y) method? Edit: Code public int Compare(string x, string y) { var sOrder = new string[] { "b", "c" }; int index_x = -1; int index_y = -1; for (int i = 0; i < sOrder.Length;i++) { if (sOrder[i] == x)

difference between IComparable and IComparer [duplicate]

落爺英雄遲暮 提交于 2019-11-27 06:07:38
This question already has an answer here: When to use IComparable<T> Vs. IComparer<T> 8 answers What is the difference between IComparable and IComparer Interfaces? Is it necessary to use this interface always with Array.Sort() method nawfal As the name suggests, IComparable<T> reads out I'm comparable . IComparable<T> when defined for T lets you compare the current instance with another instance of same type. IComparer<T> reads out I'm a comparer, I compare . IComparer<T> is used to compare any two instances of T , typically outside the scope of the instances of T . As to what they are for

When to use IComparable<T> Vs. IComparer<T>

强颜欢笑 提交于 2019-11-27 02:41:42
I'm trying to figure out which of these interfaces I need to implement. They both essentially do the same thing. When would I use one over the other? Well they are not quite the same thing as IComparer<T> is implemented on a type that is capable of comparing two different objects while IComparable<T> is implemented on types that are able to compare themselves with other instances of the same type. I tend to use IComparable<T> for times when I need to know how another instance relates to this instance. IComparer<T> is useful for sorting collections as the IComparer<T> stands outside of the

Use own IComparer<T> with Linq OrderBy

故事扮演 提交于 2019-11-26 11:52:57
I have a generic List<MyClass> where MyClass has a property InvoiceNumber which contains values such as: 200906/1 200906/2 .. 200906/10 200906/11 200906/12 My list is bound to a BindingList<T> which supports sorting with linq: protected override void ApplySortCore( PropertyDescriptor property, ListSortDirection direction) { _sortProperty = property; _sortDirection = direction; var items = this.Items; switch (direction) { case ListSortDirection.Ascending: items = items.OrderByDescending(x => property.GetValue(x)).ToList(); break; case ListSortDirection.Descending: items = items

difference between IComparable and IComparer [duplicate]

大憨熊 提交于 2019-11-26 11:51:58
问题 This question already has answers here : When to use IComparable<T> Vs. IComparer<T> (8 answers) Closed last year . What is the difference between IComparable and IComparer Interfaces? Is it necessary to use this interface always with Array.Sort() method 回答1: As the name suggests, IComparable<T> reads out I'm comparable . IComparable<T> when defined for T lets you compare the current instance with another instance of same type. IComparer<T> reads out I'm a comparer, I compare . IComparer<T>

When to use IComparable<T> Vs. IComparer<T>

若如初见. 提交于 2019-11-26 10:07:44
问题 I\'m trying to figure out which of these interfaces I need to implement. They both essentially do the same thing. When would I use one over the other? 回答1: Well they are not quite the same thing as IComparer<T> is implemented on a type that is capable of comparing two different objects while IComparable<T> is implemented on types that are able to compare themselves with other instances of the same type. I tend to use IComparable<T> for times when I need to know how another instance relates to

Use own IComparer<T> with Linq OrderBy

佐手、 提交于 2019-11-26 03:28:41
问题 I have a generic List<MyClass> where MyClass has a property InvoiceNumber which contains values such as: 200906/1 200906/2 .. 200906/10 200906/11 200906/12 My list is bound to a BindingList<T> which supports sorting with linq: protected override void ApplySortCore( PropertyDescriptor property, ListSortDirection direction) { _sortProperty = property; _sortDirection = direction; var items = this.Items; switch (direction) { case ListSortDirection.Ascending: items = items.OrderByDescending(x =>