icomparer

Should we extend Comparer<T> or implement IComparer<T>

前提是你 提交于 2021-01-02 05:09:35
问题 What is the best practice in C# starting from version 4.0 when writing a comparer class : a. Should we inherit from Comparer abstract class ? or b. Should we implement IComparer interface. What are the pros and cons? 回答1: I would recommend that you extend the Comparer<T> class instead of implementing the IComparer<T> interface, as does Microsoft (see first reference below). Now, if you want your object itself (whatever T is) to be able to compare against itself, it can implement the

Implementing custom IComparer<> (with example)

一个人想着一个人 提交于 2020-06-22 18:58:23
问题 Ive just written the following code, which will order strings by their native string.Compare() but allow a collection of exceptions (in this case customPriority ) that will place priority over the default string.Compare() function. It all seems a bit long winded, I was wondering if there was something built into .NET to allow this? var unorderered = new[] { "a", "b", "c", "x", "y", "z" }; var ordered = unorderered.OrderBy(a => a, new CustomStringComparer()); //expected order y,x,a,b,c,z class

Advantages/Disadvantages of different implementations for Comparing Objects

只谈情不闲聊 提交于 2020-01-22 08:48:06
问题 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>

Implementing generic IComparer in VB

旧城冷巷雨未停 提交于 2020-01-15 05:32:09
问题 I am trying to create a class implementing the generic IComparer of my own class "Stellungen" (which translates to positions, like on a chess or checkers board). This is what I got: Private Class comparer(Of Stellung) Implements System.Collections.Generic.IComparer(Of Stellung) Public Function Compare(x As Stellung, y As Stellung) As Integer Implements System.Collections.Generic.IComparer(Of Stellung).Compare End Function End Class Problem is: inside the function I have no access to any

How can I sort (Custom Sort) list of Dictionary entry by value

被刻印的时光 ゝ 提交于 2020-01-13 14:04:38
问题 My hashtable contains (key, Values[]) e.g: myHashtable[keys, Values[]] myHashtable.Add[1, Value1]; myHashtable.Add[2, Value2]; myHashtable.Add[3, Value3]; myHashtable.Add[4, Value4]; myHashtable.Add[5, Value5]; Where Value1; Value2, value3, value4, and value5 is as follows. Value1[name = "Smith"] Value1[Title= "Mr"] Value1[Salary = 1000] Value1[Identity = "S"] Value2[name = "Peter"] Value2[Title= "Mr"] Value2[Salary = 1000] Value2[Identity = "A"] Value3[name = "Tom"] Value3[Title= "Mr"]

How can I sort (Custom Sort) list of Dictionary entry by value

时光毁灭记忆、已成空白 提交于 2020-01-13 14:03:12
问题 My hashtable contains (key, Values[]) e.g: myHashtable[keys, Values[]] myHashtable.Add[1, Value1]; myHashtable.Add[2, Value2]; myHashtable.Add[3, Value3]; myHashtable.Add[4, Value4]; myHashtable.Add[5, Value5]; Where Value1; Value2, value3, value4, and value5 is as follows. Value1[name = "Smith"] Value1[Title= "Mr"] Value1[Salary = 1000] Value1[Identity = "S"] Value2[name = "Peter"] Value2[Title= "Mr"] Value2[Salary = 1000] Value2[Identity = "A"] Value3[name = "Tom"] Value3[Title= "Mr"]

Implementing custom IComparer with string

心不动则不痛 提交于 2020-01-09 03:49:04
问题 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" }

Passing an IComparer parameter to custom LINQ OrderBy extension method

陌路散爱 提交于 2020-01-05 05:39:06
问题 After a good dose of Googling and trying some things and not finding/getting the desired result I decided to post this question. I have a custom made OrderBy extension method and now when performing the OrderBy operation I'd like to pass an AlphanumComparator like this: return divergences.OrderBy(sort, new AlphanumComparator()); Here's the extension method: public static IQueryable<T> OrderBy<T>(this IQueryable<T> collection, GridSortOptions sortOptions, AlphanumComparator comparer = null) {

Where is the inconsistency in this Icomparer that is causing a null reference?

为君一笑 提交于 2020-01-02 01:56:42
问题 I'm receiving a null object in my custom IComparer implementation despite no null entries in the collection it is being applied to. My understanding is this can be caused by inconsistencies in the IComparer implementation. I cannot spot where this could be happening in the following code. For reference the intent is that these are sorted by the 'correct' property first, then if they are the same, it sorts based on the 'tiebreakerDelta' property, which sorts closest to zero without going over.

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

蹲街弑〆低调 提交于 2019-12-29 08:39:21
问题 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. 回答1: 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