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
3
votes
4 answers
Sorting 16 elements with IComparer does at least 100000 comparisions
At least 100000 comparisions was needed when I tried to sort with IComparer.
Why?
public class PeriodComparer: IComparer
{
int num = 0;
public int Compare(string x, string y)
{
var year1 = int.Parse(x.Substring(2));
…

Anders Lindén
- 6,839
- 11
- 56
- 109
3
votes
3 answers
How to compare multiple object values against each other?
Assume i have a object with the following values in it (also please note i do not wish to use a datetime object for this, just the following values below and i wish to solve this in the comparer itself):
int year;
int month;
int day;
int sec;
int…

Neaer
- 33
- 1
- 3
3
votes
3 answers
How to properly sort a string-number column of a DataTable
I am trying to sort a string-number column, e.g. N1, N10, N100, N2 and I am expecting the results N1, N2, N10, N100 but the sorting doesn't work, I am getting the same values N1, N10, N100, N2 in the same order.
I wrote the following code.
static…

ehh
- 3,412
- 7
- 43
- 91
3
votes
2 answers
When will a Comparer make Sort throw an ArgumentException?
The documentation for Sort says that Sort will throw an ArgumentException if "The implementation of comparer caused an error during the sort. For example, comparer might not return 0 when comparing an item with itself."
Apart from the example given,…

Brian Rasmussen
- 114,645
- 34
- 221
- 317
3
votes
1 answer
How can I sort (Custom Sort) list of Dictionary entry by value
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,…

MJA
- 35
- 1
- 5
3
votes
1 answer
custom icomparer error - The type arguments cannot be inferred from the usage
I'm trying to use IComparer with a generic type.
The code below generates the following error: "The type arguments cannot be inferred from the usage. Try specifying the type arguments explicitly."
If I remove the custom comparer from the OrderBy…

The_Capn
- 33
- 2
3
votes
2 answers
Use IComparer Asynchronously in C#
We've upgraded our code base to use async/await, but many previously working methods now crash as a result of the new asynchronous code (HttpContext = null being a common issue). I'm not sure how to get around this IComparer.Compare() method. Any…

VanAlan
- 65
- 6
3
votes
2 answers
How to sort a list of objects with IComparable and IComparer
I'm trying to implement the same example of this link but more oriented on the number of dependent kids.
http://www.codeproject.com/Articles/42839/Sorting-Lists-using-IComparable-and-IComparer-Inte
So I have 3 employees with A: 0, B: 0, C: 2…

Maximus Decimus
- 4,901
- 22
- 67
- 95
3
votes
3 answers
C# using IComparable<> and IComparer
I'm having a bit of trouble properly using IComparable<> interface. I've also created a class that implements IComparer<>. I pretty much copied the exact coding example from a book but reworked it to fit my situation but it doesn't seem to function…

khmer2040
- 159
- 1
- 5
- 16
3
votes
2 answers
What is wrong with this Custom Compare function
I was trying to debug a problem and ran into this issue. Maybe somebody can explain it to me. This is the code in question
public int Compare(CustomClass rt1, CustomClass rt2)
{
if (rt1 == null & rt2 == null)
return 0;
…

Kevin
- 7,162
- 11
- 46
- 70
3
votes
3 answers
C# Sorting/comparing items
I have a class (Patch) that I want to have sorted so I implemented IComparer.
However, it needs to be sorted depending on how the user wants it, e.g.:
- key1, key2, key3
- key1, key3, key2
For each key compare I have written a IComparer class,…

Michel Keijzers
- 15,025
- 28
- 93
- 119
3
votes
3 answers
Is there a reasonable scenario for a stateful IComparer?
I have never written a stateful IComparer with a default constructor. All standard library implementations which I've checked in Reflector are stateless as well. Therefore, I'd like to assume that I can freely cache IComparer like…

Alexey Romanov
- 167,066
- 35
- 309
- 487
2
votes
6 answers
C# sorting a arraylist with figures
Hello i do want a sort an array, that contains this:
String[] info = new String[5]{"6,j", "7,d", "12,s", "4,h", "14,s" };
But if i use this:
Array.Sort(info);
The output becomes:
"7,d"
"6,j"
"4,h"
"14,s"
"12,s"
But i wont the output to…

flaimme
- 99
- 3
- 13
2
votes
1 answer
IComparer problem in vb.net but same working in C#
I found running code for Natural Sort Alpha-numeric like Windows Explorer in C# and https://dotnetfiddle.net/kBP7EW
Dmitry Bychenko wrote some very good code in C# which is 100% working but somehow is not working in VB.NET. I'm unable to understand…

Ram
- 131
- 8
2
votes
1 answer
PriorityQueue in C# doesn't work with custom comparer
Can someone help me understand why this doesn't work and how can make it work?
PriorityQueue pq =
new PriorityQueue(new EstimateCompare());
public class EstimateCompare: IComparer
{
public int…

AlreadyLost
- 767
- 2
- 13
- 28