Questions tagged [icomparer]

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

228 questions
2
votes
6 answers

Generic IComparer for sorting different objects in different properties

I'm trying to sort an array of objects with IComparer. I wrote the code but it works only with the particular object. e.g.: for this class public class Cars { public string Name { get; set; } public string Manufacturer { get; set; } …
Mikho
  • 31
  • 1
  • 3
2
votes
3 answers

C# SortedList with Byte Array as Key

I want to compare byte arrays used as keys in a SortedList public SortedList myList = new SortedList(); the problem is, that I cant add entries into myList, because .NET doesnt know how to compare two elements of the…
Live
  • 120
  • 9
2
votes
1 answer

Passing an IComparer parameter to custom LINQ OrderBy extension method

After a good dose of Googling and trying some things and not finding/getting the desired result I decided to post this question. I have a custom made OrderBy extension method and now when performing the OrderBy operation I'd like to pass an…
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
2
votes
1 answer

Natural Sorting Issue

I have been trying to use the code borrowed from here to sort out a collection of a custom object with my application. Generally the sorting works fine until i encounter the following strings D-016.0, D-016., D-016.00, D-016.000 001, D-016.000…
Mo Patel
  • 2,321
  • 4
  • 22
  • 37
2
votes
2 answers

Is there a quick, easy and free way to auto-generate an IComparable implementation for a C# class?

I know that Resharper can code-gen a comparer implementation (and does a really great job of it), but I'm looking for a free way to do the same. I've got a customer who is not likely to invest 250$ per developer, but the developers would still like…
Assaf Stone
  • 6,309
  • 1
  • 34
  • 43
2
votes
2 answers

Comparing 3 Objects

I am coding a generic Binary Tree in C#. In one portion of my application I need to sort in order of distance. from A to C and B to C. something like this: if ((A->C) == (B->C)) return 0; else if((A->C) < (B->C)) return -1; else return…
Henrique Rocha
  • 411
  • 5
  • 13
2
votes
4 answers

Ordering ObservableCollection with Linq and IComparer

I want to order alphabetically an ObservableCollection because I don't wanna have to create another binding. I've seen we could use Linq and the method OrderBy. I would like also to use a personal IComparer because I will have to organize complex…
Sheamus
  • 223
  • 1
  • 5
  • 15
2
votes
2 answers

Implementing generic IComparer in VB

I am trying to create a class implementing the generic IComparer of my own class "Stellungen" (which translates to positions, like on a chess or checkers board). This is what I got: Private Class comparer(Of Stellung) Implements…
Ralf
  • 538
  • 1
  • 6
  • 17
2
votes
4 answers

Is it possible in C# to provide an IComparer implementation and somehow have it "compiled" into the application at runtime?

Hi everyone I am not sure if what I want to do is even remotely possible but I am going to do my best to explain it and would really appreciate any suggestions / ideas, Imagine I have :- public class Attribute{ object Value; …
lostinwpf
  • 633
  • 2
  • 9
  • 29
1
vote
2 answers

How to sort an object in a list on a non-unique value?

I'm trying to categorize articles by stored keywords. I have a list of keywords for a category, and I want an article to get assigned a category that has the most keyword count. For Each keyword As String In category.Keywords category.tempCount…
JustinKaz
  • 620
  • 3
  • 10
  • 30
1
vote
1 answer

Sorting issue within a hierarchy of elements in C#

I've been working on a page in ASP.NET. This page behaves exactly like a forum. A user can reply to entries. A reply is represented as an ItemReply. When a user replys to an item, the ParentID is set to the ID of the ItemReply the user is responding…
Villager
  • 6,569
  • 22
  • 65
  • 87
1
vote
7 answers

C# IComparer is returning unexpected result when comparing same strings

I have a situation where all of my list members have same ID (Id is string and not Integer). As part of business rules I need to sort the list in ascending order. My original implementation is quite similar to below. I was expecting to get unchanged…
1
vote
2 answers

How to make one Comparer call another one?

Context I have a list of Movies, which have a Title and a DurationInMinutes: Movie m1 = new Movie("Jurassic World Dominion", 149); Movie m2 = new Movie("Top Gun Maverick", 152); Movie m3 = new Movie("Docter Strange in the Multiverse of Madness",…
Camelopardalus
  • 499
  • 1
  • 4
  • 20
1
vote
4 answers

SortedList not sorting on key - VB.NET

I have a the need for key value pair that I wish to sort so I decided to use a SortedList instead of a HashTable. I am adding the data in the order below to my SortedList which is the order I need it in Key | Value …
w4ymo
  • 312
  • 2
  • 8
  • 21
1
vote
1 answer

SortedSet inserting element out of sort

I’ve written an implementation of A* that relies on sorting nodes by their F score in a sortedSet. The sorting, in some cases, seems to insert a Node object at the 'Min' value when its compared 'F' value is actually the second lowest rather than the…
Celeste Soueid
  • 162
  • 1
  • 7