Defines a generalized comparison method that a value type or class implements to create a type-specific comparison method for ordering instances.
Questions tagged [icomparable]
264 questions
7
votes
4 answers
IComparable in C#
I have an object called Shape which contains a public int[,] coordinate { get; set; } field.
I have a separate class which has a collection of Shape objects. At a particular point, I wish to check:
if(shapes.Contains(shape))
{
//…

Subby
- 5,370
- 15
- 70
- 125
6
votes
2 answers
Generics and Implementing IComparable
I am very new to generics and I am trying to write a simple class which will be generic but also allow sorting of some description on a string member variable.
At the moment I have a basic class but when I try to implement the interface member…

CSharpened
- 11,674
- 14
- 52
- 86
6
votes
2 answers
Problem comparing items implementing IComparable
I am working on a extension method where it finds the min item by specific selector. Below the code
public static T MinBy(this IEnumerable src, Func selector) where K : struct, IComparable, IConvertible
{
var min =…

RameshVel
- 64,778
- 30
- 169
- 213
6
votes
1 answer
How to send an array of ints to a function that accepts array of IComparable?
I have a C# function that accepts an array of IComparable
public static void sort(IComparable[] a){//...}
If I send an array of strings to this function it is accepted, but an array of ints is not accepted even though the structure Int32 extends…

Horea
- 75
- 4
6
votes
3 answers
Implementing IComparable
This might be a trivial question, but I didn't find any information about this: is it "harmful" or considered bad practice to make a type T implement IComparable (T and S being two different types)?
Example:
class Foo : IComparable
{
…

Luc Touraille
- 79,925
- 15
- 92
- 137
6
votes
3 answers
Algorithm speed-up using List.Sort and IEnumerable
For my project, I first load an image from file, and put every pixel into a 2D pixels[,] array. Then, I want to examine each pixel and split them up into "bins" based on how they are colored, and then sort each bin. So I have a Bin object, which…

XenoScholar
- 187
- 3
- 10
5
votes
3 answers
Sort a list of interface objects
I have a couple of classes that implement an interface, IFoo. I need to display a list of objects of these classes in a table, which I'd like to be able to sort by any arbitrary column in the table. So, the data source for the table is a…

Josh K
- 93
- 1
- 5
5
votes
2 answers
Generic constraints -- I'm not sure how to fix this situation with an either/or case
Basically I have the following:
public static bool IsBetween(this T value, T a, T b)
where T : IComparable
{
...
}
public static bool IsBetween(this T value, T a, T b)
where T : IComparable
{
...
}
The problem is I can't…

myermian
- 31,823
- 24
- 123
- 215
5
votes
3 answers
How can I use "is" to test if a type supports IComparable?
I want to check if a type supports IComparable before sorting it, but I've found that checking if a type supports the IComparable interface using "is" does not always give me the correct answer. For example, typeof(int) is IComparable returns false,…
user650635
5
votes
2 answers
Is Valid Generic Binary Search Tree with Recursion
To check the validity of a Binary Search Tree, I use a method. But the method always returns false, when tested against a valid Binary Search Tree.
Online Demo Here
The code
public class Program
{
public static void Main()
{
…

Bayu
- 437
- 2
- 10
- 19
5
votes
2 answers
Why doesn't IComparable inherit from IComparable?
In most places I read that it is a good idea to inherit from both IComparable and IComparable in your classes to provide compatibility with non-generic collections. My question is why IComparable doesn't inherit from IComparable by default…

Andrew Petukhov
- 51
- 2
5
votes
2 answers
Assuming this != null when implementing IComparable
I have an object of type T which implements IComparable. Is it okay when implementing bool Equals (T obj) to ommit the check if (ReferenceEquals(this, null)) { DoSomething() }? Can I assume that since the function could be called this is already…

Miguel
- 51
- 1
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
5 answers
Native C# support for checking if an IEnumerable is sorted?
Is there any LINQ support for checking if an IEnumerable is sorted? I have an enumerable that I want to verify is sorted in non-descending order, but I can't seem to find native support for it in C#.
I've written my own extension method using…

Mikkel R. Lund
- 2,336
- 1
- 31
- 44
5
votes
1 answer
MSTest - why is there no AreEqual(object, object, IComparer)?
Writing UnitTests with MSTest I want to assert the equality of a return value vs. the one I'm expecting.
Expected type is a custom type that does not implement the IComparable interface nor the IEquatable interface, thats why I want to give…

buddybubble
- 1,269
- 14
- 33