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
3 answers
How to pass function to compare items?
I would like to pass a comparer into a function and not sure if this is possible with Javascript. There is an IComparer concept in C# that does this. Can this be done in Javascript?
function someAction (comparer) {
var newList = [];
…

TruMan1
- 33,665
- 59
- 184
- 335
0
votes
1 answer
How to implement the equivalent functionality to a custom IComparer for Linq to Entities OrderBy()?
How can I implement the equivalent to a custom IComparer for Linq to Entities OrderBy()?
A custom comparer is not supported in Linq to Entities as MSDN documentation states:
Most of the LINQ ordering methods are supported in LINQ to Entities, with…

Jim
- 3
- 3
-1
votes
2 answers
IComparer C# + generics
How I can write make method Compare in RoomComparerByVolume ?
"Define a generic class RoomComparerByVolume<> implementing IComparer interface.
Impose a constraint on the type argument so that it should implement the IShape interface.
This comparer…

hekeemje
- 101
- 3
-1
votes
3 answers
Sorting based on multiple fields using IComparer
I use the below code to sort List list.
Here is the sort order :
PriorityScore
MName
CName
FName
It works as expected.
public int Compare(DataAccessViewModel x, DataAccessViewModel y)
{
if (x == null || y == null)
{
…

Pரதீப்
- 91,748
- 19
- 131
- 172
-1
votes
1 answer
Using IComparer to compare 2 IEnumerables with 1 comp obj
I'm looking for a way to create a method which should compare for example 2 strings with 1 comparable string and return 1, 0 or -1 (if the value is greater than, equal to, or less than). The method should look like this:
int…

RRuseva
- 25
- 5
-1
votes
1 answer
HashSet to be sorted using a custom IComparer without using LINQ
Scenario
I have a custom class (called FeedData) that would collected in a HashSet
internal struct FeedData
{
internal string ID;
internal Vector2d Location;
}
I now have a custom IComparer that would help sort the…

Filip
- 155
- 2
- 14
-1
votes
1 answer
sort lowercase letters before uppercase with Icomparer
I have a ListCollectionView which holds a collection of scene objects and I need to sort it by scene Number (Scene.SceneNumber) which is a string containing numbers and letters and I need to sort it in a very specific order. First by number then by…

Phil
- 561
- 2
- 16
- 29
-1
votes
1 answer
C# Using IComparer to sort alphanumeric list
I recently stumbled upon IComparer and I think it can help me sort my alphanumeric string list.
// "STATE-ShipSpeed-0001"
List myList = new List{
"TX-S3-1005",
"CA-S3-1205",
"NV-S2-1001",
"LA-S5-1015",
"VA-S1-1305",
};
From what…

snapplex
- 851
- 3
- 13
- 27
-1
votes
1 answer
Make a Class it's Own Comparer
I have a class that looks like this
public class Foo
{
public int A { get; set; }
public List Bs { get; set; }
}
And a comparer that looks like this
public class FooComparer : IComparer
…

Sachin Kainth
- 45,256
- 81
- 201
- 304
-1
votes
1 answer
IEqualityComparer for class with many properties neither unique value
How do implementation for IEqualityComparer for this class?
The ID property is not unique. Neither properties has unique values.
The entity has 7 properties.
[Serializable()]
public class ServidorSeleccionadoDto
{
[XmlAttribute()]
public int…

Kiquenet
- 14,494
- 35
- 148
- 243
-1
votes
1 answer
Is this how to write a Thread-Safe IComparable?
I have a simple class and I want to make it thread-safe. The class needs to implement IComparer. I know that implementing the int CompareTo(T other) in a thread-safe way is not straight-forward. It's easy to allow a deadlock if I don't lock in…

user2023861
- 8,030
- 9
- 57
- 86
-2
votes
2 answers
IComparer does not implement interface member - Error CS0738
I am trying to sort two objects by one of their properties (.Transaction.topLeftX, an integer) using the following code to create a comparer to use in a Sort method:
public class RespComp : IComparer
{
public…

SimonKravis
- 553
- 1
- 3
- 24
-2
votes
2 answers
How does icomparer implementing class decide sorting order on the array items?
How does the return value of IComparer.Compare function decide the order of sorting operation? What is the significance of return values in deciding the sorting order? How does the flow of Array.Sort() happen?
For example, :
int[] ii = new int[3] {…

psin
- 59
- 8
-2
votes
1 answer
C# Override Compare method of IComparer Interface
I have a class which overwrites the Compare method of IComarer class.
Can you please explain me the code portion "return v1v2.CompareTo(v2v1)* -1;"?
What * is representing here?Why we are subtracting the value by 1?
public class ValueComparator :…

Debanjan
- 69
- 1
- 8
-3
votes
2 answers
How to implement a custom sort using IComparer in C#
I have a list of names, and I want to implement a custom ordering by implementing the IComparer interface. This custom sort must check the first names of two items, and if they are the same, it should group them together. For example, the final…

Vahid
- 5,144
- 13
- 70
- 146