IComparer is an interface provided by the .NET framework, used in conjunction with the Array.Sort and Array.BinarySearch methods. It provides a way to customize the sort order of a collection. It contains a single Compare method that compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other. There is also a generic version of this interface. Supported in .NET 4, 3.5, 3.0, 2.0, 1.1, 1.0. Source: MSDN
Questions tagged [icomparer]
228 questions
10
votes
2 answers
Quick IComparer?
Before I go reinventing the wheel, is there some framework way of creating an IComparer from a Func?
EDIT
IIRC (it's been a while) Java supports anonymous interface implementations. Does such a construct exist in C#, or are delegates…

spender
- 117,338
- 33
- 229
- 351
9
votes
4 answers
In the List.Sort() method, is an item ever compared to itself?
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…

ForeverLearnNeverMaster
- 895
- 1
- 10
- 17
9
votes
2 answers
Should derived classes hide the Default and Create static members inherited from Comparer?
I am writing an IComparer implementation by deriving from the Comparer class, as recommended by MSDN. For example:
public class MyComparer : Comparer
{
private readonly Helper _helper;
public MyComparer(Helper helper)
{
…

Douglas
- 53,759
- 13
- 140
- 188
9
votes
2 answers
What should IComparer return to indicate "keep the existing sort order"
I'm implementing a custom comparer in order to apply a custom sort order for items in various views.
Some of the time I'm finding that I want to maintain the existing order of items, in this case what should I return from my Compare method…

Justin
- 84,773
- 49
- 224
- 367
8
votes
1 answer
Why does IComparer require you to define IComparer.Compare(Object x, Object y) and not just Compare(Object x, Object y)?
I'm fairly new to C# (6 months on the job experience), but it seems pretty similar to Java so I feel right at home.
However, today I tried implementing the IComparer interface and wondered why it was giving me an error:
public class…

RickySpanish
- 501
- 1
- 3
- 10
8
votes
5 answers
using Comparer to sort IEnumerable in C# by different fields
I have a list of an object which need to be sorted depending on three different properties of the object.
Example
CLass Object1{ Property1 , Property2, Property3}
ListObj = IEnumerable
Foreach ( item in ListObj){
if (item.Property1…

user99322
- 1,157
- 2
- 15
- 26
8
votes
1 answer
Where is the inconsistency in this Icomparer that is causing a null reference?
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…

WillyCornbread
- 837
- 1
- 12
- 21
8
votes
3 answers
Modify List.Contains behavior
I have a List with the class MyObj : IComparable. I wrote the method CompareTo in the MyObj class per the IComparable interface, but when I use the List.Contains(myObjInstance) it returns false when it should be true.
I'm not sure I'm…

Lancelot
- 2,417
- 12
- 39
- 46
8
votes
3 answers
LINQ OrderBy anonymous object with projection comparer
Ive been trying to get OrderBy in a LINQ statement to work with an anonymous object but have failed by now.
I checked these already:
Anonymous IComparer implementation
C# linq sort - quick way of instantiating IComparer
How to sort an array of…

Joanna Derks
- 4,033
- 3
- 26
- 32
7
votes
1 answer
IEqualityComparer and singleton
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 =…

igor
- 171
- 1
- 1
- 4
7
votes
6 answers
Index Of Longest Run C#
I am trying to solve this question:
Write a function that finds the zero-based index of the longest run in a string. A run is a consecutive sequence of the same character. If there is more than one run with the same length, return the index of the…

Kob_24
- 592
- 1
- 10
- 26
6
votes
3 answers
Why doesn't List.BinarySearch() have overloads that take Comparison in addition to IComparer?
I want to use List.BinarySearch() with a custom item type. The custom type does not implement IComparable; instead I have several static Comparison functions that I call because at various points I want to sort the list on different criteria.…

Craig W
- 4,390
- 5
- 33
- 51
6
votes
4 answers
Is there a way to derive IEqualityComparer from IComparer?
TL;DR I'm looking for a way to obtain IEqualityComparer from IComparer, no matter which datatype is T, including case-insensitive options if T is string. Or I need a different solution for this problem.
Here's full story: I'm implementing…

Endrju
- 2,354
- 16
- 23
6
votes
3 answers
Effectively disable Sort() within a CompareTo() override?
The CompareTo() method for my class is dynamic, and can range from a simple comparison to comparisons on a number of columns. This is all determined at run time, and it works great.
But in some cases, I want any attempt to sort a collection of my…

richardtallent
- 34,724
- 14
- 83
- 123
6
votes
3 answers
Implementing custom IComparer<> (with example)
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…

maxp
- 24,209
- 39
- 123
- 201