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
113
votes
5 answers

What's the difference between IComparable & IEquatable interfaces?

Both the interfaces seem to compare objects for equality, so what are the major differences between them?
SoftwareGeek
  • 15,234
  • 19
  • 61
  • 78
111
votes
8 answers

When to use IComparable Vs. IComparer

I'm trying to figure out which of these interfaces I need to implement. They both essentially do the same thing. When would I use one over the other?
Micah
  • 111,873
  • 86
  • 233
  • 325
100
votes
8 answers

How to compare values of generic types?

How do I compare values of generic types? I have reduced it to a minimal sample: public class Foo where T : IComparable { private T _minimumValue = default(T); public bool IsInRange(T value) { return (value >=…
gstercken
  • 3,223
  • 3
  • 20
  • 15
88
votes
8 answers

How to Implement IComparable interface?

I am populating an array with instances of a class: BankAccount[] a; . . . a = new BankAccount[] { new BankAccount("George Smith", 500m), new BankAccount("Sid Zimmerman", 300m) }; Once I populate this array, I would like to sort it by…
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
64
votes
6 answers

difference between IComparable and IComparer

What is the difference between IComparable and IComparer Interfaces? Is it necessary to use this interface always with Array.Sort() method
Raghav55
  • 641
  • 1
  • 6
  • 3
63
votes
6 answers

What problem does IStructuralEquatable and IStructuralComparable solve?

I've noticed these two interfaces, and several associated classes, have been added in .NET 4. They seem a bit superfluous to me; I've read several blogs about them, but I still can't figure out what problem they solve that was tricky before .NET…
thecoop
  • 45,220
  • 19
  • 132
  • 189
41
votes
3 answers

IComparable and IComparable

Should I implement both IComparable and the generic IComparable? Are there any limitations if I only implement one of them?
Mark Menchavez
  • 1,651
  • 12
  • 15
38
votes
2 answers

Why do I have to overload operators when implementing CompareTo?

Let's say I have a type that implements IComparable. I would have thought it's reasonable to expect that the operators ==, !=, >, <, >= and <= would "just work" automatically by calling CompareTo but instead I have to override them all if I want to…
Andy
  • 10,412
  • 13
  • 70
  • 95
38
votes
4 answers

Should IEquatable, IComparable be implemented on non-sealed classes?

Anyone have any opinions on whether or not IEquatable or IComparable should generally require that T is sealed (if it's a class)? This question occurred to me since I'm writing a set of base classes intended to aid in the implementation of…
Phil
  • 2,675
  • 1
  • 25
  • 27
18
votes
6 answers

How do I use the IComparable interface?

I need a basic example of how to use the IComparable interface so that I can sort in ascending or descending order and by different fields of the object type I'm sorting.
Spencer Ruport
  • 34,865
  • 12
  • 85
  • 147
14
votes
3 answers

IComparable behaviour for null arguments

I'm implementing IComparable and IComprable in one of my classes. Is there any recommendation on how the CompareTo method in each case should behave when given a null argument? Should it return a positive number or throw an ArgumentNullException?…
G S
  • 35,511
  • 22
  • 84
  • 118
14
votes
1 answer

Why is C# Array.BinarySearch so fast?

I have implemented a very simple binarySearch implementation in C# for finding integers in an integer array: Binary Search static int binarySearch(int[] arr, int i) { int low = 0, high = arr.Length - 1, mid; while (low <= high) { …
Daniel
  • 10,641
  • 12
  • 47
  • 85
13
votes
2 answers

What sorting algorithm does the .NET framework implement

Could anyone please advise when implementing something like IComparable in .NET what sorting algorithm does .NET use to actually sort the underlying data? Also is the algorithm used customizable or selectable?
Maxim Gershkovich
  • 45,951
  • 44
  • 147
  • 243
13
votes
3 answers

Collection that maintains sort order C#

I have a class Foo which contains a list of objects: List. Each Bar has a property which they can be ordered on (of type TimeSpan, representing a duration), and Bar is an immutable object - that is, the duration does not change over the…
08Dc91wk
  • 4,254
  • 8
  • 34
  • 67
12
votes
5 answers

Interface constraint for IComparable

When I want to constraint the type T to be comparable, should I use: where T : IComparable or where T : IComparable I can't get my head around if #2 makes sense. Anyone can explain what the difference would be?
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
1
2 3
17 18