icomparer

IEqualityComparer and singleton

三世轮回 提交于 2019-12-10 13:30:31
问题 I was wondering if there is possibility to use singleton as comparerObject in for example Distinct ?? Let's say that I have a list of element and I need to use distinct function on that list. Normally I would do that this way var result = list.Distinct(new ListElementComparer); ListElementComparer is a class which implements IEqualityComparer interface. However let's assume that I will be using code mentioned above quite frequently for example that way . List<List<Element>> elementList = new

When will a Comparer make Sort throw an ArgumentException?

笑着哭i 提交于 2019-12-06 03:32:14
The documentation for Sort says that Sort will throw an ArgumentException if "The implementation of comparer caused an error during the sort. For example, comparer might not return 0 when comparing an item with itself." Apart from the example given, can anyone tell me when this would otherwise happen? The sort algorithm (QuickSort) relies on a predictable IComparer implementation. After a few dozen layers of indirection in the BCL you end up at this method: public void Sort(T[] keys, int index, int length, IComparer<T> comparer) { try { ... ArraySortHelper<T>.QuickSort(keys, index, index +

C# IComparer<T> standard usage question

假装没事ソ 提交于 2019-12-05 16:42:58
I have a question with whether or not this is a standard for using IComparer in C#. Say I have a situation in which there are three Person objects: P1, P2, and P3. Say I call the Compare method passing in P1 and P2 and the result is 0. This essentially means the two people should be categorized as equal. Now say I call the Compare method passing in P2 and P3 and the result for that is 0 as well. Again, this means the two people are equal. Logically speaking, one can assume P1 and P3 are equal as well; however, the Compare method could be implemented however someone decides to implement it. So

Modify List.Contains behavior

╄→гoц情女王★ 提交于 2019-12-04 00:57:30
问题 I have a List<MyObj> with the class MyObj : IComparable . I wrote the method CompareTo in the MyObj class per the IComparable interface, but when I use the List<MyObj>.Contains(myObjInstance) it returns false when it should be true . I'm not sure I'm understanding how I need to proceed to make sure the List uses my custom comparison method when calling then Contains function. Here is my compareTo implementation: #region IComparable Members public int CompareTo(object obj) { MyObj myObj =

Advantages/Disadvantages of different implementations for Comparing Objects

我的未来我决定 提交于 2019-12-03 01:40:36
This questions involves 2 different implementations of essentially the same code. First, using delegate to create a Comparison method that can be used as a parameter when sorting a collection of objects: class Foo { public static Comparison<Foo> BarComparison = delegate(Foo foo1, Foo foo2) { return foo1.Bar.CompareTo(foo2.Bar); }; } I use the above when I want to have a way of sorting a collection of Foo objects in a different way than my CompareTo function offers. For example: List<Foo> fooList = new List<Foo>(); fooList.Sort(BarComparison); Second, using IComparer: public class BarComparer :

C# lambda expressions and IComparer

∥☆過路亽.° 提交于 2019-12-02 22:43:54
I am using lambda expressions to sort and search an array in C#. I don't want to implement the IComparer interface in my class, because I need to sort and search on multiple member fields. class Widget { public int foo; public void Bar() { Widget[] widgets; Array.Sort(widgets, (a, b) => a.foo.CompareTo(b.foo)); Widget x = new Widget(); x.foo = 5; int index = Array.BinarySearch(widgets, x, (a, b) => a.foo.CompareTo(b.foo)); } } While the sort works fine, the binary search gives a compilation error Cannot convert lambda expression to type 'System.Collections.IComparer<Widget>' because it is not

How can I make my generic comparer (IComparer) handle nulls?

偶尔善良 提交于 2019-12-01 15:36:06
I'm trying to write a generic object comparer for sorting, but I have noticed it does not handle the instance where one of the values it's comparing is null. When an object is null, I want it to treat it the same as the empty string. I've tried setting the null values to String.Empty but then I get an error of "Object must be of type String" when calling CompareTo() on it. public int Compare(T x, T y) { PropertyInfo propertyInfo = typeof(T).GetProperty(sortExpression); IComparable obj1 = (IComparable)propertyInfo.GetValue(x, null); IComparable obj2 = (IComparable)propertyInfo.GetValue(y, null)

How can I make my generic comparer (IComparer) handle nulls?

不问归期 提交于 2019-12-01 15:19:16
问题 This question was migrated from Software Engineering Stack Exchange because it can be answered on Stack Overflow. Migrated 8 years ago . I'm trying to write a generic object comparer for sorting, but I have noticed it does not handle the instance where one of the values it's comparing is null. When an object is null, I want it to treat it the same as the empty string. I've tried setting the null values to String.Empty but then I get an error of "Object must be of type String" when calling

Modify List.Contains behavior

冷暖自知 提交于 2019-12-01 03:54:33
I have a List<MyObj> with the class MyObj : IComparable . I wrote the method CompareTo in the MyObj class per the IComparable interface, but when I use the List<MyObj>.Contains(myObjInstance) it returns false when it should be true . I'm not sure I'm understanding how I need to proceed to make sure the List uses my custom comparison method when calling then Contains function. Here is my compareTo implementation: #region IComparable Members public int CompareTo(object obj) { MyObj myObj = (MyObj)obj; return String.Compare(this.Symbol, myObj.Symbol, true); } #endregion Note the Symbol property

How to use custom IComparer for SortedDictionary?

三世轮回 提交于 2019-12-01 03:10:12
I am having difficulties to use my custom IComparer for my SortedDictionary<>. The goal is to put email addresses in a specific format (firstnam.lastname@domain.com) as the key, and sort by last name. When I do something like this: public class Program { public static void Main(string[] args) { SortedDictionary<string, string> list = new SortedDictionary<string, string>(new SortEmailComparer()); list.Add("a.johansson@domain.com", "value1"); list.Add("b.johansson@domain.com", "value2"); foreach (KeyValuePair<string, string> kvp in list) { Console.WriteLine(kvp.Key); } Console.ReadLine(); } }