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
4
votes
2 answers

SortedList that just takes IComparable

I have an interface IScriptItem that implements IComparable. In my eyes it would seem enough to have IComparable items in order to have a sorted anything. But all I can find is Dictionaries, Hashtables and SortedLists that are actually…
Boris Callens
  • 90,659
  • 85
  • 207
  • 305
3
votes
3 answers

How to find max value in a column in a datarow[] ?

I have a simple problem but I just don't understand any of the examples I find here or in MSDN. (I'm still new to C# and all the datasets functions). I have a datatable "tblRoom" which its columms are "building","apartment" and "room" and they're…
Shay Krainer
  • 35
  • 1
  • 1
  • 5
3
votes
1 answer

PowerShell IComparable with subclasses

Let's assume we have these 3 classes: Class BaseClass : System.IComparable { [int] $Value BaseClass([int] $v) { $this.Value = $v } [int] CompareTo($that) { If (-Not($that -is [BaseClass])) { Throw…
Michele Locati
  • 1,655
  • 19
  • 25
3
votes
2 answers

Implement IComparable with strings

I have class Employee and I need to implement IComparable and use CompareTo method to sort employees by name. From what I've seen, I have to return 1, -1, and 0, but how do I use the strings? Here's what I have. class Employee :…
Xoax
  • 77
  • 4
  • 13
3
votes
1 answer

Comparing generic fields

I have some generic types, like the following: public struct Tuple { ... } public struct Tuple { ... } etc. These should in theory be able to compare themselves against other values of the same type, so that I can write the…
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
3
votes
4 answers

Problems with implementing generic IEnumerator and IComparable

I'm working on an AVL Tree. The tree itself seems to be working but I need a iterator to walk through the values of the tree. Therefore I tried to implement the IEnumerator interace. Unfortunately I get a compile time error implementing IEnumerator…
user341877
  • 237
  • 2
  • 12
3
votes
1 answer

How do I Override GetHashCode and CompareTo for a Discriminated Union in F#?

I have a simple F# discriminated union that combines a bool, a string, and a float. I want to override the Object.Equals(arg) of this union so that I can put in an epsilon to account for precision errors when checking for float equality. The…
user2023861
  • 8,030
  • 9
  • 57
  • 86
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
1 answer

What happens when ==, CompareTo(), and Equals() do not agree?

I have a program I wrote some years back to find "good" binary operators for bytes; byte A is left multiplied by byte B to yield byte C. The operator is defined as 256x256 byte matrix. A stripped down version of the class implementation is…
John Washburn
  • 568
  • 1
  • 5
  • 11
3
votes
5 answers

Internal working of the Sort() and CompareTo() methods

I've been trying to figure out how the CompareTo() method works internally and I failed. I've searched this site and read some posts, and I think I've seen all there is to see in MSDN about this subject and I just don't seem to get it. An MSDN…
JaviML
  • 41
  • 1
  • 6
3
votes
3 answers

How to use a generic class for System.Math methods in C#

I am trying to achieve something similar to the following code snippet. As the red line indicates Math.Min for IComparable does not seem to work. I need to use Math.Min or Math.Max for this generic class. The T is going to be either int or…
DynamicScope
  • 695
  • 9
  • 26
2
votes
3 answers

Sort an ArrayList of Objects in C#

How can I sort an ArrayList of objects? I have implemented the IComparable interface while sorting the ArrayList, but I am not getting the required result. My code sample: public class Sort : IComparable { public string Count { get; set; } …
Srikanth
  • 683
  • 3
  • 16
  • 23
2
votes
2 answers

why do we need Icomparable interface?

Please clarify, we can have our own method to compare two objects instead implementing interface.Also what is the significance of interfaces?
Srikanth
  • 980
  • 3
  • 16
  • 30
2
votes
2 answers

FxCop and IComparable/IComparable

I'm currently investigating the use of FxCop with one of our existing projects and am getting an odd result. The output displays a small number of breaches of the 'Override methods on comparable types' rule stating "'Log' should override Equals…
DilbertDave
  • 3,406
  • 3
  • 33
  • 42