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
0
votes
1 answer
How to create custom common number-text comparer in C# to sort numeric/string list?
I have number-text list and I want to create common custom comparer logic to sort this list(s) using C#. E.g.
var numericList = new List{"100", "--", "-0.98", "N/A", "0.00", "-888"};
var stringList = new List {"Smith", "--", "Peter",…

Sumit Deshpande
- 2,155
- 2
- 20
- 36
0
votes
0 answers
How to compare two grids on column values
We have two grids located one above other one, we need to compare the column values of the grids by using the column names.
I want to achieve the code in such a manner that:
//Comparing column value using column name.
if…

rp4361
- 433
- 1
- 5
- 20
0
votes
1 answer
Convert and Object Class to IComparable in c#
I am implementing a binary tree using the following code:
class Binary:Binary
where T: IComparable
{
But I'm also looking to use class objects in the tree:
Binary Bin = null;
Bin = new Binary();
This obviously doesnt work because…
0
votes
1 answer
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…

JSprang
- 12,481
- 7
- 30
- 32
0
votes
0 answers
IComparer not being called
I have a problem where my custom IComparer is not being called. I am thinking that maybe it might have to do with me expecting for it to work with different objects? Why is it not being called?
The Icomparer:
public class MyComparer :…

Afonso
- 127
- 1
- 10
0
votes
1 answer
How do you use a custom type for a dictionary key?
I have a custom class which uses generics.
I need to use this class as the key of a dictionary as shown in the code example below:
I am able to hit the overridden Object.GetHashCode method, but i'm not sure how to proceed from there. Please help,…

Charles Okwuagwu
- 10,538
- 16
- 87
- 157
0
votes
1 answer
Is there a way to use ">" operator on a "T As IComparable(Of T)", instead of "a.GreaterThan(b)", or a way to simplify my wrapper line?
Consider:
MyClass(Of T As {New, IComparable(Of T)})
Sub MySub(a As T, b As T)
If a.CompareTo(b) > 0 Then
....
End If
End Sub
End Class
This can be made more readable by defining:
Public Module MyModule
…

ToolmakerSteve
- 18,547
- 14
- 94
- 196
0
votes
2 answers
IComparable Namespace Issue in C# Program
I'm working on a program that uses IComparable, and I'm following the code in the book, but it's not clear on where to put it exactly. I placed it in my Driver program, but in the original program, the main class is underlined, saying
"Error 1 …

user2970463
- 43
- 7
0
votes
1 answer
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…

Frank Schwieterman
- 24,142
- 15
- 92
- 130
0
votes
2 answers
How do you implement IComparable to sort an array of a custom type?
I wrote a program for my C# class and need to figure out how to implement IComparable to be able to sort an array of a custom type. It compiles with no errors, but throws an exception when run:
System.InvalidOperationException: Failed to compare…

user3806529
- 31
- 1
- 1
0
votes
5 answers
Issues with a list containing multiple data types
For a university task, I've been asked to write a program containing a class that finds the largest and smallest values of some data type. We must also satisfy the following requirements:
Exploits generics with a single deferred type with an…

SuperDicko
- 278
- 2
- 4
- 15
0
votes
1 answer
Comparing for sorting in different ways
I have a class that implements IComparable. It works but the comparison is static, i.e. it's always the same ordering it achieves. What would be a good method to introduce comparison by a parameter, i.e. if we have:
class Poo : IComparable {
…

Konrad Viltersten
- 36,151
- 76
- 250
- 438
0
votes
1 answer
Custom Comparator to sort list of states, counties and zip codes
I'm having hard time to write comparator to sort list of custom objects with the following string fields:
1. Branch Class --> Can be state, county or zip code and used to identify which data field is it; can't be empty
2. State --> State Name,…

Mohamed Farag
- 139
- 1
- 5
- 15
0
votes
1 answer
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 where T : IComparable
{
protected class Bar : IComparable
…

dreadwail
- 15,098
- 21
- 65
- 96
0
votes
2 answers
How to implement IComparable to sort numerical and non-numerical string
How to implement IComparable to sort numerical and non-numerical string.
Firstly, I want to get the Min and Max value in the "list".
It is asking me to implement ICompareable. " At least one object must implement IComparable"
Can anyone help me?…

Daoming Yang
- 1,325
- 3
- 20
- 41