IComparer is an interface provided by the .NET framework, used in conjunction with the Array.Sort and Array.BinarySearch methods. It provides a way to customize the sort order of a collection. It contains a single Compare method that compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other. There is also a generic version of this interface. Supported in .NET 4, 3.5, 3.0, 2.0, 1.1, 1.0. Source: MSDN
Questions tagged [icomparer]
228 questions
0
votes
1 answer
Sort rows of dynamic datatable by multiple columns using a IComparer typed class
I have an extension method that am trying to sort the rows of a datatable by multiple columns using a IComparer typed class for at least one of the sortings.
The datatable is created with columns whose name and datatype are only known at runtime. …

Billyhole
- 1,688
- 3
- 20
- 32
0
votes
1 answer
Sort files by date but folders by name
In attempt to show folders sorted by name at the top and then files underneeth sorted by date I have made this PropertyGroupDescription and IComparer
using (ItemCollectionView.DeferRefresh())
{
var dataView =…

Daniel
- 1,064
- 2
- 13
- 30
0
votes
2 answers
Abstract class using IComparer
I want to create and abstract class A that implements IComparer so that I can implement different forms of comparing in the subclasses and later on do:
A sortBy = new B();
A sortBy2 = new C();
I'm new in C# and I don't quite get how to do that. I…

moondaisy
- 4,303
- 6
- 41
- 70
0
votes
0 answers
How to compare two numeric values (SByte, Double) stored as Objects in .NET 2.0?
In my custom sorting algorithm, I need to compare numeric types stored as Objects in an array. We can have SByte, Double, Int32, etc values mixed in one array. How can I compare two values of that array in my IComparer implementation?
An example.…

TecMan
- 2,743
- 2
- 30
- 64
0
votes
2 answers
VB .Net IComparer sorting string that include blanks numerically
I have a datagridview databound string column that contain string numbers 0-99 and blank, nothing.
I wish to sort this column numerically 0 to 99 with either the blank nothing cells at the beginning or at the end.
I use…

Mark
- 1,360
- 3
- 20
- 37
0
votes
1 answer
Error:not implement interface member 'System.IComparable.CompareTo(object)
I have a class with .IComparable interface,but got error when complied.
Please help take a look and give me a solution.
Error 1 'phyl.DnaSequenceTree.Node' does not implement interface member 'System.IComparable.CompareTo(object)'
using…

Wang Xiguang
- 15
- 1
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
2 answers
IComparer.CompareTo(Object a, Object b)
I have an Arraylist with 10 or more Level object. These objects all have a Level ID.
I try now to write an IComparer.CompareTo(Level a,Level b).
I don't really know how to Implement this. Never worked with IComparer and when I read the MSDN Libary I…

Hansanho
- 295
- 1
- 3
- 13
0
votes
3 answers
The type or namespace name 'T' could not be found while Implementing IComparer Interface
I am trying to implement IComparer Interface in my code
public class GenericComparer : IComparer
{
public int Compare(T x, T y)
{
throw NotImplementedException;
}
}
But this throws an error
Error 10 The type or namespace…

None
- 5,582
- 21
- 85
- 170
0
votes
4 answers
Optimizing a bin-placement algorithm
Alright, I've got two collections, and I need to place elements from collection1 into the bins (elements) of collection2, based on whether their value falls within a given bin's range.
For a concrete example, assume I have a sorted collection…

jaws
- 1,952
- 4
- 20
- 27
0
votes
1 answer
Implement IComparer(Of DateTime) on list(of X) property
I want to sort all items in the list of the property reviews of the class CompanyTotalReview by the property reviewdatetime in descending order.
Here's what I have:
Dim allReviews As RestService.CompanyTotalReview = rst.getReviews(type, objectId,…

Adam
- 6,041
- 36
- 120
- 208
0
votes
1 answer
Reversed Sort order when implementing IComparer.Compare
I'm scratching my head a bit with this.
I'm using winforms to build a business application and I'm using a lot of ListViews. I've got one particular list which, when populated with items can set the forecolor property of the item to either red or…

BenM
- 4,218
- 2
- 31
- 58
0
votes
2 answers
Change the comparer for a ListBox
I have a list box that contains a list of WorkItems (from TFS). I want to add items to be selected, but it is failing to identify the items as the same.
Here is my code
public void SelectQueryResultItem(WorkItem item)
{
…

Vaccano
- 78,325
- 149
- 468
- 850
0
votes
3 answers
Sorting a list by numeric value, alphabetic value and then a symbol
Hi i have been racking my brains to figure a way to do this but i cant. Lets say I have the following list:
1
2
'
@
r
r2
r1
I would like it to be sorted to the following order
1
2
r
r1
r2
'
@
I have the following code (cant figure out where to do…

RTaylorFord
- 13
- 1
- 2