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
5
votes
1 answer
Array of HashSets with comparer in C#
as the title says, i have an array of hashsets, but i don't know how apply to them a comparer. Like this:
//This Works:
public HashSetUpdateList = new HashSet(new CellComparer());
//This Does not work:
public…

Yann
- 180
- 3
- 14
5
votes
1 answer
Swashbuckle custom string comparer not applied for order groups of actions
In Swashbuckle there is a setting called OrderActionGroupsBy which is supposed to change the ordering within the API, but nothing I do is working and I'm can't determine whether this is a Swashbuckle problem, or due to my IComparer any idea what I'm…

nastassiar
- 1,545
- 2
- 24
- 43
5
votes
2 answers
Custom Icomparer with argument
How would an IComparer that needs an argument be implemented (might not be relevant but I'm using it on Linq query)?
I suppose it should be called like this:
ListOfObjectsToSort.orderBy(x => x, myCustomComparer(argument));
And this is what i found…

Afonso
- 127
- 1
- 10
5
votes
2 answers
IComparer issue
I have a weird issue and I don't have a clue to track the reason. I will try to descript my issue clearly.
I have a RTree class, in this class, I want to compare two rectanlge (here I called envelope, it contains minX, minY, maxX, maxY), so we have…

Howard
- 3,638
- 4
- 32
- 39
5
votes
4 answers
Implementing IComparer combining multiple Linq OrderBy's
My problem is that I always want to order a collection of objects in a certain fashion.
For example:
class foo{
public string name {get;set;}
public DateTime date {get;set;}
public int counter {get;set;}
}
...
IEnumerable dosomething(foo[]…

maxp
- 24,209
- 39
- 123
- 201
5
votes
6 answers
"Cannot convert to IComparer"
I have the following IComparer defined for boxed RegistryItem objects:
public class BoxedRegistryItemComparer : IComparer

Odrade
- 7,409
- 11
- 42
- 65
5
votes
3 answers
Sorting List with Custom object by another List using IComparer
My question is about the IComparer interface, I never worked with it before, so I hope you can help me set up everything right.
I have to use the interface to sort an list of own objects by the exact sequence of another List.
I couldn't find…

Tom Pfennig
- 53
- 1
- 4
5
votes
3 answers
C# generic class and EqualityComparer
Could anyone explain me what's wrong in the following class declaration:
private class PriorityQueueEntry :
IComparer>
where…

Heisenbug
- 38,762
- 28
- 132
- 190
4
votes
4 answers
C# IComparer standard usage question
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…

Nick
- 2,265
- 7
- 30
- 34
4
votes
1 answer
Using IComparer.Compare(T,T) in C#
I've been trying to make a generic Reverse Priority Queue, but int the EnQueue, but I still can't manage the errors that come with using IComparer.
The Error: Error 1 An object reference is required for the non-static field, method, or…

Sherbi7y
- 155
- 1
- 2
- 6
3
votes
1 answer
List.Sort IComparer performance
I'm trying to sort a pair of int arrays (int[] a; int[] b;)
If I use Array.Sort(a,b) then the performance is great.
However, I'd prefer to use a List<> and load the int pairs in a struct.
I can get this to work using Array.Sort() with an overload…

rob
- 210
- 3
- 8
3
votes
1 answer
C# SortedList, getting value by key
I have SortedList in descending order.
public class MyComparer : IComparer
{
public int Compare(int x, int y)
{
if (x.CompareTo(y) > 0)
return -1;
return 1;
}
}
class…

HelloWorld
- 1,061
- 2
- 15
- 25
3
votes
2 answers
How to compare Tuples with a custom Comparer for entries, but the default Comparer for the Tuple?
I want to sort a List>, i.e. a list of tuples, where each tuple contains a certain amount of vertices.
Vertex is a custom class, List and Tuple are from System.
I already have several Comparers that provide a way to compare two…

Aaron
- 181
- 1
- 8
3
votes
2 answers
C# Comparer.Default.Compare and Comparer.Default doesn't return -1, 0 or 1
What's the reason behind all "numeric" comparers (such as Comparer.Default, Comparer.Default, etc.) returning either -1, 0 or 1, but Comparer.Default and Comparer.Default will return the difference between the two compared…

Ludovic C
- 2,855
- 20
- 40
3
votes
2 answers
Sort a list in a specific order comparer
I have a list of strings and I need to write an IComparer implementation to sort them in a specific order.
My current implementation:
public enum TimeBucket
{
[Description("0D")]
ZeroDay,
[Description("1D")]
OneDay,
…

Skyuppercut
- 332
- 3
- 14