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
2 answers

How to use List.Sort and Comparision(of T) to sort Descensing/Ascending?

I have a MyObject; myObjects as List(Of MyObject) and a delegate Comparison(Of MyObject) that uses a lot of comparison functions (ByA, ByB, ByC etc) à la: Shared Function CompareMyObjectsByName(x As MyObject, y As MyObject) As Integer Return…
serhio
  • 28,010
  • 62
  • 221
  • 374
2
votes
3 answers

Should a custom comparer for strings allow for null values

I'm looking at someone elses code for a custom comparer that compares strings. I'm noticing that it will fall over if at least one of the string parameters is null. The compare returns -1, 0 or 1 based on the result of the comparison. Should code…
Coder 2
  • 4,761
  • 12
  • 40
  • 43
2
votes
2 answers

How to sort a list by his own properties and properties from another list?

In my app I want to show football tables from different leagues for example the English Premier League and the Spanish Primera Division. I created a SeasonTableTeamObject where I save all values. (TeamID, points, goals, conceded goals, playedGames,…
basti12354
  • 2,490
  • 4
  • 24
  • 43
2
votes
1 answer

Is performance of List.Sort() by Comparison is better than custom IComparer?

I am creating a custom sorting a list of integers. I tried both: using Comparison and custom IComparer: with comparison: public int ComparisonMethod(int x, int y) { //just for testing return 1;// just for test …
Nhan Phan
  • 1,262
  • 1
  • 14
  • 32
2
votes
2 answers

order collection by given sort order

I have a List of strings that I want to bring in a certain order. Let's say the list contains a random amount of the strings "A", "B" and "C". And I have another List of strings containing the sort order. For Example: For…
EffinEf
  • 23
  • 3
2
votes
4 answers

C# Compare rating values

My company has a rating system for it's users (string format right now). The system is a typical, A+, A, A-, B+, B...F I would like to be able to compare them so that I can build rules like: if UserJoe.Rating > Ratings.B give him special offer. I…
FailedUnitTest
  • 1,637
  • 3
  • 20
  • 43
2
votes
1 answer

Ensuring a valid IComparer is being used

I have a class (Foo) which is required to compare T type objects, however T may not always implement IComparable and the constructor should be able to be used with a null comparer param. To catch this on creation of Foo I attempted the…
2
votes
2 answers

Using IComparer for grouping and sorting

I am trying to use IComprarer to a complex sort. I have this complex type (for sake of simplicity, three ints): ID | A | B 1 | 1 | 10 2 | 3 | 20 3 | 1 | 30 4 | 3 | 5 5 | 2 | 15 And i need to Sort by B, but i need to keep A together. B can be…
Rafael
  • 345
  • 3
  • 16
2
votes
1 answer

Why do some lists of tuples are sorted and not others?

I need to sort 28 Tuple. Chars are in A-Z, '+', '*'. The ASCII order is: *, +, A...Z and I want to get this order: A...Z, +, *. To do that I use this Comparer: public class AlphabeticPaginationComparer : IComparer> { public int…
2
votes
1 answer

C# Using IComparer to sort x number of columns

I would like to be able to sort data by x columns, where x is not constant. Context: I have a DataGridView (unbound), in which there are a number of rows. As I want to sort by more than one column, I have created a class which implements IComparer…
Your_Unequal
  • 246
  • 1
  • 5
  • 17
2
votes
2 answers

IComparable and OrderBy. Trying to sort Poker hands with C#

I am attempting to create a simple program that analyzes the poker hands. Given n hands/players and the community cards (Texas hold 'em) I would like to determine the winner(s). However, my test is failing when I have two exact results - it only…
nullable
  • 2,513
  • 1
  • 29
  • 32
2
votes
1 answer

Should I cache a comparer in a static field?

I have added the following property to my ApplicationUser class, which is supposed to return current user on top of other results. public static IComparer IdComparer { get { return Comparer.Create((x, y) => { …
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
2
votes
2 answers

Can IComparer be used to hash a list as it is populated?

I'm new to trying to use the IEnumerable interfaces. I've always just written custom hash sorting rather than trying to use native syntax because I was somewhat confused by the implementation. I'm trying to determine if I can assemble a List in…
ThisHandleNotInUse
  • 1,135
  • 1
  • 10
  • 23
2
votes
4 answers

CompareTo() method doesn't work

I have a Class named Person which implement IComparable generic Interface . I have a generic list which contain Person Object and I assign my list to an array and I'm sorting the list but I'm taking the follwing error. error: {"Failed to…
erhan urun
  • 115
  • 9
2
votes
2 answers

Using Comparison() comparison with the three arguments overload

I have a List() and I want to sort it with a custom comparer function. I made: public int MyCompare(Point p1, Point p2) { ... } // In my main // ... points_.Sort(MyCompare); // ... I works, all right. Now I want to sort everything but…
Francesco Bonizzi
  • 5,142
  • 6
  • 49
  • 88