icomparer

LINQ orderby vs IComparer

一笑奈何 提交于 2019-11-30 13:51:10
问题 I would like to know what is better to use. IComparer class and Compare method for sort or LINQ orderby on List. Both works fine but which one is better for large lists. 回答1: I would choose LINQ for two reasons. LINQ queries are generally shorter and easier to read. If you really do have a large number of elements, Linq also gives you the ability to scale out to multiple CPU cores by using PLinq, which might help you out significantly. I would expect performance to be roughly similar for a

How do I use a custom comparer with the Linq Distinct method?

白昼怎懂夜的黑 提交于 2019-11-30 09:12:42
问题 I was reading a book about Linq, and saw that the Distinct method has an overload that takes a comparer. This would be a good solution to a problem I have where I want to get the distinct entities from a collection, but want the comparison to be on the entity ID, even if the other properties are different. According to the book, if I have a Gribulator entity, I should be able to create a comparer like this... private class GribulatorComparer : IComparer<Gribulator> { public int Compare

LINQ orderby vs IComparer

五迷三道 提交于 2019-11-30 08:36:48
I would like to know what is better to use. IComparer class and Compare method for sort or LINQ orderby on List. Both works fine but which one is better for large lists. I would choose LINQ for two reasons. LINQ queries are generally shorter and easier to read. If you really do have a large number of elements, Linq also gives you the ability to scale out to multiple CPU cores by using PLinq, which might help you out significantly. I would expect performance to be roughly similar for a single-threaded implementation, if you consider that the lambda expression in your OrderBy clause compiles to

Shuffle using IComparer

↘锁芯ラ 提交于 2019-11-30 06:59:06
First of all, I do know about the Fisher-Yates shuffle. But lets say for arguments sake that I want to allow the user to pick a sort option from a Dropdown list. This list would include a "Random" option. Based on the result of their selection I just want to substitute in an IComparer instance for my sort. What would the IComparer look like? Google brings up a plethora of flawed results that all take this form: public class NaiveRandomizer<T> : IComparer<T> { private static Random rand = new Random(); public int Compare(T x, T y) { return (x.Equals(y))?0:rand.Next(-1, 2); } } However, that

In the List<T>.Sort() method, is an item ever compared to itself?

守給你的承諾、 提交于 2019-11-29 14:01:10
If I pass in a custom IComparer to an instance of a List's Sort() method, will the comparer's Compare(x,y) method ever be called with the same item? ie. Is it possible that Compare(x,x) may be called. Edit: More interested in the case where items of the list are distinct. I wrote a test program to try it out. It looks like it actually does Compare() the same element to itself (at least Compare() is called for the same item twice). In this program, Compare() is called with arguments (2, 2). using System; using System.Collections.Generic; static class Program { class MyComparer : Comparer<int> {

How do I use a custom comparer with the Linq Distinct method?

我与影子孤独终老i 提交于 2019-11-29 13:18:47
I was reading a book about Linq, and saw that the Distinct method has an overload that takes a comparer. This would be a good solution to a problem I have where I want to get the distinct entities from a collection, but want the comparison to be on the entity ID, even if the other properties are different. According to the book, if I have a Gribulator entity, I should be able to create a comparer like this... private class GribulatorComparer : IComparer<Gribulator> { public int Compare(Gribulator g1, Gribulator g2) { return g1.ID.CompareTo(g2.ID); } } ...and then use it like this... List

Shuffle using IComparer

本秂侑毒 提交于 2019-11-29 09:25:33
问题 First of all, I do know about the Fisher-Yates shuffle. But lets say for arguments sake that I want to allow the user to pick a sort option from a Dropdown list. This list would include a "Random" option. Based on the result of their selection I just want to substitute in an IComparer instance for my sort. What would the IComparer look like? Google brings up a plethora of flawed results that all take this form: public class NaiveRandomizer<T> : IComparer<T> { private static Random rand = new

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

谁说胖子不能爱 提交于 2019-11-28 19:04:36
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 possible to muck around like this and pass an algorithm (read delegate) where a single-method interface is

Using lambda expression in place of IComparer argument

你说的曾经没有我的故事 提交于 2019-11-28 08:51:40
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 Timothy Shields As Jeppe points out, if you're on .NET 4.5, you can use the static method Comparer<T>.Create . If not, this is an

Using IComparer for sorting

▼魔方 西西 提交于 2019-11-27 21:03:01
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(); Points.Sort(c); The code errors out. Apparently it is expecting IComparer<Point> as argument to sort