icomparable

Implementing IComparable

I am implmenting the IComparable to sort like typed objects. My question is why does it cast type person to int32? The array's Sort() seems to cast each type in the array to the type that I am using for comparison. Comparable: public class Person:IComparable { protected int age; public int Age { get; set; } public int Compa...

C# Binary Search on 2 indexes

Hello, I have an object with attributes ; startIndex, endIndex I am able to do binary search based on startIndex by implementing the following : int IComparable.CompareTo(object obj) { Repeat r = (Repeat)obj; return this.startIndex.CompareTo(r.startIndex); } However with the same Repea...

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 = new ArrayList(); arrName.Add("BOB") arrName.Add("JOHN") arrName.Add("TOM") arrName.Add(...

Comparing Nested Inner Classes Within Generic Class

I am crafting a nested inner class within a generic class and I'm unsure if I'm writing this properly. Is there anything glaringly wrong here? Here's the code: public class Foo<T> where T : IComparable<T> { protected class Bar : IComparable { private KeyValuePair<T, string> _keyval; public Bar(KeyValuePair<T, s...

C# - What's the difference between IComparable & IEquatable interfaces?

Hi folks, both the interfaces seem to compare objects for equality, so what's the major differences between them? TIA ...

C# - How to implement multiple comparers for an IComparable<T> class?

I have a class that implements IComparable. public class MyClass : IComparable<MyClass> { public int CompareTo(MyClass c) { return this.whatever.CompareTo(c.whatever); } etc.. } I then can call the sort method of a generic list of my class List<MyClass> c = new List<MyClass>(); //Add stuff, etc. c.Sort(); ...

C#: IComparable implementation private

Hello I'm new to C# so this might be a really dump question: I implemented IComparable in my class and want to test it with NUnit. But the CompareTo-Method is marked as private and thus not accessible from the test. What's the reason for this and how can I fix this? The IComparable: public class PersonHistoryItem : DateEntity,ICompa...

XAttribute implementing IComparable during tests, but not when live

I have some code below that is throwing an exception in integration environments but not in my unit tests. Basically I'm sorting some XML elements (linq-2-sql XElement) by an attribute value. All the nodes have the attribute defined. IEnumerable<XElement> elements = ...; // elementes are of the form<recipe name="something"> elements....

Implementing IComparable<NotSelf>

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<S> (T and S being two different types)? Example: class Foo : IComparable<int> { public int CompareTo(int other) { if (other < i) return -1; if (other >...

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 Comparable. That way generics actually do their job - of being typesafe. import java.util.TreeSe...

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 list of items, I get the following order out: A1 A2 B3 B2 B1 It has obviously worked...

IComparable not included when serializing in WCF

Hey guys, I have a list i'm filling at server side. It's a list of "User", which implements IComparable. Now when WCF is serializing the data, i guess it's not including the CompareTo method. This is my Object class : [DataContract] public class User : IComparable { private string e164, cn, h323; private int id; private D...

Sort a 2D Points List (first by X and then Y)

I am trying to sort a List of 2D Points first by x co-ordinate and then by y co-ordinate. I implemented the IComparer interface as follows: class PointComparer : IComparer<Point> { public int Compare(Point x, Point y) { if (x.Y != y.Y) { return x.Y - y.Y; } else { r...

[C#] Problems with implementing generic IEnumerator and IComparable

Hi all! 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 and IComparable. First the code and below that the error. class AvlTreePreOrderE...

How would you go about making a List<> comparable?

I am using a Telerik GridView, and having an issue trying to sort a column that is made of of a List<>. In this forum entry, the Telerik team states that the grid can sort IComparable and group/filter IEquatable<> no matter of the Silverlight version. In the xaml below, you will see the four columns that I have in my grid. The SVOs co...

Implementing custom comparison with CustomComparison and CustomEquality in F# tuple

Hello folks! 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 the VS gave to me I arrived to something like this: The tree type I used to h...

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 supposed to be able to handle null values. Quote: "By definition, any object compares grea...

IComparable - Call different sorts?

I have a DTO that I am using to process transactions. To ensure that it is processing in the correct order, I am using iComparable and sorting the List(of T) of the DTO. That works great. However I just got another requirement that the customer wants the output in a different order... is there a way to allow me to have two different sort...

Sorting IComparable Object Performance in C#

I have designed a class which is basically nothing but an object which stores a large number of properties of a piece of data. I have implemented IComparable in the class. Objects of the class are instantiated and stored in a List. There is an added level of complexity in that certain fields of the object determine on which fields I...

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 4. What use are IStructuralEquatable and IStructuralComparable? ...