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
111
votes
8 answers
When to use IComparable Vs. IComparer
I'm trying to figure out which of these interfaces I need to implement. They both essentially do the same thing. When would I use one over the other?

Micah
- 111,873
- 86
- 233
- 325
100
votes
3 answers
Using lambda expression in place of IComparer argument
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 :…

haughtonomous
- 4,602
- 11
- 34
- 52
79
votes
8 answers
Pass a lambda expression in place of IComparer or IEqualityComparer or any single-method interface?
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…

Water Cooler v2
- 32,724
- 54
- 166
- 336
64
votes
6 answers
difference between IComparable and IComparer
What is the difference between IComparable and IComparer Interfaces? Is it necessary to use this interface always with Array.Sort() method

Raghav55
- 641
- 1
- 6
- 3
54
votes
4 answers
Using IComparer for sorting
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…

Aadith Ramia
- 10,005
- 19
- 67
- 86
46
votes
5 answers
Use own IComparer with Linq OrderBy
I have a generic
List
where MyClass has a property InvoiceNumber which contains values such as:
200906/1
200906/2
..
200906/10
200906/11
200906/12
My list is bound to a
BindingList
which supports sorting with linq:
protected override…

Jürgen Steinblock
- 30,746
- 24
- 119
- 189
22
votes
4 answers
C# lambda expressions and IComparer
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()
…

Justin Morgan
- 2,427
- 2
- 16
- 19
19
votes
2 answers
Implementing custom IComparer with string
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,…

maxp
- 24,209
- 39
- 123
- 201
18
votes
5 answers
Advantages/Disadvantages of different implementations for Comparing Objects
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…

Kevin Crowell
- 10,082
- 4
- 35
- 51
15
votes
2 answers
Should we extend Comparer or implement IComparer
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?

Sebastian Widz
- 1,962
- 4
- 26
- 45
14
votes
3 answers
LINQ orderby vs IComparer
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.

senzacionale
- 20,448
- 67
- 204
- 316
14
votes
1 answer
How do I use a custom comparer with the Linq Distinct method?
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…

Avrohom Yisroel
- 8,555
- 8
- 50
- 106
11
votes
7 answers
Shuffle using IComparer
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…

Joel Coehoorn
- 399,467
- 113
- 570
- 794
11
votes
4 answers
How can I make my generic comparer (IComparer) handle nulls?
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…

NickG
- 9,315
- 16
- 75
- 115
11
votes
2 answers
How to use custom IComparer for SortedDictionary?
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…

Magnus Johansson
- 28,010
- 19
- 106
- 164