icomparer

Using IComparer for sorting

一笑奈何 提交于 2019-12-28 02:49:31
问题 I am trying to use an IComparer to sort a list of Points. Here is the IComparer class: public class CoordinatesBasedComparer : IComparer { public int Compare(Object q, Object r) { Point a = (p)q; Point b = (p)r; if ((a.x == b.x) && (a.y == b.y)) return 0; if ((a.x < b.x) || ((a.x == b.x) && (a.y < b.y))) return -1; return 1; } } In the client code, I am trying to using this class for sorting a list of points p (of type List<Point> ): CoordinatesBasedComparer c = new CoordinatesBasedComparer()

C# IComparer<T> standard usage question

狂风中的少年 提交于 2019-12-22 07:57:13
问题 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

How to use custom IComparer for SortedDictionary?

一笑奈何 提交于 2019-12-19 05:23:14
问题 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");

Using lambda expression in place of IComparer argument

a 夏天 提交于 2019-12-17 18:33:52
问题 Is it possible with C# to pass a lambda expression as an IComparer argument in a method call? eg something like var x = someIEnumerable.OrderBy(aClass e => e.someProperty, (aClass x, aClass y) => x.someProperty > y.SomeProperty ? 1 : x.someProperty < y.SomeProperty ? -1 : 0); I can't quite get this to compile so I'm guessing not, but it seems such an obvious synergy between lambdas and anonymous delegates that I feel I must be doing something foolishly wrong. TIA 回答1: As Jeppe points out, if

Using lambda expression in place of IComparer argument

痞子三分冷 提交于 2019-12-17 18:33:10
问题 Is it possible with C# to pass a lambda expression as an IComparer argument in a method call? eg something like var x = someIEnumerable.OrderBy(aClass e => e.someProperty, (aClass x, aClass y) => x.someProperty > y.SomeProperty ? 1 : x.someProperty < y.SomeProperty ? -1 : 0); I can't quite get this to compile so I'm guessing not, but it seems such an obvious synergy between lambdas and anonymous delegates that I feel I must be doing something foolishly wrong. TIA 回答1: As Jeppe points out, if

When will a Comparer make Sort throw an ArgumentException?

好久不见. 提交于 2019-12-12 09:56:54
问题 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? 回答1: 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

How to compare two numeric values (SByte, Double) stored as Objects in .NET 2.0?

落爺英雄遲暮 提交于 2019-12-12 06:04:56
问题 In my custom sorting algorithm, I need to compare numeric types stored as Objects in an array. We can have SByte, Double, Int32, etc values mixed in one array. How can I compare two values of that array in my IComparer implementation? An example. Let's say we have two numeric values: object var1 = (sbyte)-8; object var2 = (double)123.456; It would be nice if code like the following one worked, but it fails: IComparable cmp1 = var1 as IComparable; IComparable cmp2 = var2 as IComparable;

How do you use a custom type for a dictionary key?

孤者浪人 提交于 2019-12-12 03:13:20
问题 I have a custom class which uses generics. I need to use this class as the key of a dictionary as shown in the code example below: I am able to hit the overridden Object.GetHashCode method, but i'm not sure how to proceed from there. Please help, Thanks. Module Module2 Dim myStore As New Dictionary(Of Pair(Of Long, Integer), String) Public Function ContainsItem(id As Long, code As Integer) As Boolean Return myStore.ContainsKey(New Pair(Of Long, Integer)(id, code)) End Function Public Class

Using IntPtr with IComparer<T>

*爱你&永不变心* 提交于 2019-12-11 18:18:44
问题 Three related questions here: The IntPtr structure apparently does not implement < and > operators. Is there a way to perform this comparison without converting the structure to an int or long? Why are the < and > operators not implemented on this structure? I am enumerating windows from a 32 bit process. Windows from 64 bit processes also seem to be included, which is fine. How can I tell whether to use ToInt32 or ToInt64 on the IntPtr structure? Context: Windows 7 (64 bit), VS 2010,

Sort String Array As Int

江枫思渺然 提交于 2019-12-11 10:11:58
问题 Is there some way to use IComparer with ArrayList.Sort() to sort a group of strings as ints? 回答1: If they are all strings, why are you using an ArrayList? If you're on .Net 2.0 or later, List<string> is a much better choice. If you're on .Net 3.5 or later: var result = MyList.OrderBy(o => int.Parse(o.ToString() ) ).ToList(); 回答2: Sure. Just create the appropriate comparer that does the conversion. public class StringAsIntComparer : IComparer { public int Compare(object l, object r) { int left