Questions tagged [icomparable]

Defines a generalized comparison method that a value type or class implements to create a type-specific comparison method for ordering instances.

264 questions
5
votes
3 answers

CompareTo behaviour for double.NaN and double.NegativeInfinity

I was doing some statistical operations in C# (.Net 4) with double[] then i found some strange behavior with CompareTo method and double.NaN. when I try this code: double.NaN.CompareTo(double.NegativeInfinity) //returns -1 It means double.NaN is…
Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
5
votes
3 answers

how to handle double.NaN for Generic method accepting IComparable in C#

I have a generic GetMinimum method. It accepts array of IComparable type (so it may be string[] or double[]). in the case of double[] how can I implement this method to ignore the double.NaN values? (I'm looking for good practices) when I pass this…
Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
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
2 answers

How to Naturally Sort a DataView with something like IComparable

My DataView is acting funny and it is sorting things alphabetically and I need it to sort things numerically. I have looked all across the web for this one and found many ideas on how to sort it with ICompare, but nothing really solid. So my…
SpoiledTechie.com
  • 10,515
  • 23
  • 77
  • 100
4
votes
3 answers

F# Set using custom class

I'm trying to use Set operations with a class that I have. Every instance of this class has a unique ID. Do I need to implement the System.IComparable interface and if so how would I? type SomeClass(id : int) = member this.ID = id let someSet…
bhd739ge
  • 43
  • 3
4
votes
3 answers

Why Can I Cast an Int to IComparable?

I had a question on a quiz that asked "What is the following doing?": // Value is an int IComparable thing = (IComparable)value; Apparently the answer is boxing, but I don't know why. Why is this considered boxing and what is it doing? I was under…
user11910061
4
votes
2 answers

Generic List and IComparable Sorting Error: Can not convert lambda expression

I have implemented my own GenericList and Task classes such as: public GenericList where T: Task { public List list = new List(); .... public void Sort() { list = list.Sort((a,b) => b.Id.CompareTo(a.Id) > 0); //here I am…
user8738888
4
votes
5 answers

Where is my IComparable Implementation going wrong?

namespace SortableLists { using System; using System.Collections.Generic; public class Program { private static void Main() { var list = new List { …
Perpetualcoder
  • 13,501
  • 9
  • 64
  • 99
4
votes
4 answers

Sorting list of interface reference objects inherting ICompareable

I was having trouble with list.Sort() for a list of interface references that point to different types however the question Sort a list of interface objects provided the following solution solution interface IFoo : IComparable { int…
Jack Blackmore
  • 117
  • 1
  • 10
4
votes
2 answers

Is IComparable the best way to use to enforce unique keys in Dictionary?

I have a class MyClass, and I want to put it as the key to a Dictionary like so: Dictionary dict = new Dictionary(); I want to ensure that MyClass is a unique key, and uniqueness is specified by looking at…
user3685285
  • 6,066
  • 13
  • 54
  • 95
4
votes
4 answers

Difference between IComparable and IComparable in this search method

I know that there is a big difference between IComparable and IComparable in general, see this, but in this search method it will not make any difference, or will it? public static int Search(List a, T target) where T : IComparable { …
Dieter Meemken
  • 1,937
  • 2
  • 17
  • 22
4
votes
5 answers

Why is my List.Sort method in C# reversing the order of my list?

I have a list of items in a generic list: A1 (sort index 1) A2 (sort index 2) B1 (sort index 3) B2 (sort index 3) B3 (sort index 3) The comparator on them takes the form: this.sortIndex.CompareTo(other.sortIndex) When I do a List.Sort() on the…
Fiona - myaccessible.website
  • 14,481
  • 16
  • 82
  • 117
4
votes
1 answer

Built-in compare on discriminated unions in f#

In answering this question, I discovered the following behaviour of compare on discriminated unions. type T = A | B | C | D compare A B (* val it : int = -1 *) compare A C (* val it : int = -2 *) compare A D (* val it : int = -3 *) I was…
Søren Debois
  • 5,598
  • 26
  • 48
4
votes
3 answers

IComparable Does not Implement

I've changed from using IComparable to IComparable However I'm getting the error 'RecordCollection.Artist' does not implement interface member 'System.IComparable.CompareTo(object)' class Artist : IComparable I've added a…
4
votes
1 answer

Benefits of IComparable as Contravariant?

I have very little experience with variance, but after having read quite a bit believe that I understand at least the basic concepts (i.e. variance describes the relationship between the relationship of two types and the relationship of those two…
blf
  • 117
  • 4