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
12
votes
2 answers
Nullable generic type used with IComparable. Is it possible?
I'm trying to create a simple Clamp (so that I can bound the values of anything comparable ... mostly for number types such as int, double, etc.)
The problem is if I do the following I get an error, but according to MSDN IComparable's CompareTo is…

myermian
- 31,823
- 24
- 123
- 215
12
votes
4 answers
Reverse Sorting with IComparable
I have code like this -
List users;
protected class User : IComparable
{
public string name;
public string email;
public decimal total;
public string address;
public string company;
public string origin;
…
user3896016
11
votes
1 answer
Implementing custom comparison with CustomComparison and CustomEquality in F# tuple
I'm here to ask a specific topic - I really found few info about this on the web.
I'm implementing a F# version of Minimax algorithm. The problem I'm having now is that I want to compare Leaf of my tree (data structure below). Searching the erros…

Pedro Dusso
- 2,100
- 9
- 34
- 64
11
votes
4 answers
C# - How to implement multiple comparers for an IComparable class?
I have a class that implements IComparable.
public class MyClass : IComparable
{
public int CompareTo(MyClass c)
{
return this.whatever.CompareTo(c.whatever);
}
etc..
}
I then can call the sort method of a generic…

Gary Willoughby
- 50,926
- 41
- 133
- 199
10
votes
4 answers
Why does Java's TreeSet not specify that its type parameter must extend Comparable?
e.g. The code below throws a ClassCastException when the second Object is added to the TreeSet. Couldn't TreeSet have been written so that the type parameter can only be a Comparable type? i.e. TreeSet would not compile because Object is not…

Tarski
- 5,360
- 4
- 38
- 47
9
votes
6 answers
Compare/count values in a System.Collections.ArrayList
I'm scrubbing 5 files for a specific value. I dont anticipate any different values, BUT since this is for my own educational purposes, I would like the application to count, compare and print the most popular value.
for example:
ArrayList arrName =…

Leroy Jenkins
- 2,680
- 6
- 25
- 33
9
votes
1 answer
Why can I not use IComparable on ancestor class and compare child classes?
I'm trying to sort a list of objects using List.Sort(), but at runtime it tells me that it cannot compare elements in the array.
Failed to compare two elements in the array
Class structure:
public abstract class Parent : IComparable {
…

Thomas
- 1,512
- 3
- 12
- 37
9
votes
2 answers
List to IEnumerable
I can implicitly cast an int to a IComparable. I can also cast a List or an array to a IEnumerable.
But why can't I implicitly cast a List to a IEnumerable?
I tested this with the .net framework 4.5 and Visual Studio 2012 Ultimate.
Code to test:…

user886079
- 864
- 2
- 11
- 18
8
votes
2 answers
F# comparison vs C# IComparable
My problem, in a nutshell, is this:
What can I do about storing a tuple (or any type with a constraint of 'comparison') in a C# container that requires an IComparable?
This works:
> let x (y : 'a when 'a : comparison) = y ;;
val x : y:'a -> 'a when…

Adam Klein
- 476
- 1
- 4
- 13
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
1 answer
"At least one object must implement IComparable" for an INT? As far as I know, it does
Ok, I have a simple IEnumerable things and I want to divide it up into four equal groups.
var quarter = things.OrderBy(t => t.Foo).Count() / 4;
should do the trick, but instead I get this funkiness:
Server Error in '/' Application. At…

Scott Baker
- 10,013
- 17
- 56
- 102
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
7
votes
4 answers
Sorting IComparable objects some of which are null
Most people, when writing a refence type (class) which implements IComparable, use the convention that null is LESS than any actual object. But if you try to use the opposite convention, something interesting happens:
using System;
using…

JeppeSN
- 73
- 1
- 3
7
votes
1 answer
Why does ValueTuple use non-standard IComparable implementation?
The documentation for ValueTuple.IComparable.CompareTo(Object) says it returns:
0 if other is a ValueTuple instance; otherwise, 1 if other is null.
This makes the IComparable implementation seemingly useless other than perhaps not breaking code…

Herman
- 2,738
- 19
- 32
7
votes
1 answer
Is there an interface in C# for interval-scaled values?
I'm making an interval collection extension of the famous C# library C5. The IInterval interface defines an interval with comparable endpoints (irrelevant members removed):
public interface IInterval where T : IComparable
{
T Low { get; }
…

Mikkel R. Lund
- 2,336
- 1
- 31
- 44