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
1
vote
1 answer

Does .NET have a case-ignoring implementation of IComparer?

I have Dictionary. I want to get my int value by string key, but ignore string case. I just figured out, that I can use Dictionary constructor with IComparer parameter and Dictionary will use it to compare keys. But do I…
Kamil
  • 13,363
  • 24
  • 88
  • 183
1
vote
1 answer

How to represent different types of documents in a logical order

I need to represent different types of documents in a logical order. So I have a list of objects with IDs. I need to make a comparer which orders these objects by a "manual" rule. Order by id must be 2,32,5,12,8,43... and so on. I use LINQ…
podeig
  • 2,597
  • 8
  • 36
  • 60
1
vote
3 answers

Sorting list of Objects using LINQ and private IComparer?

My assignment is to have a functionality which enables app user to sort RegisteredUser objects according to their filed/property values. Assistant class inherits RegisteredUser. I would like somehow to be able to utilize lambdas and LINQ and to sort…
Wrapper
  • 794
  • 10
  • 25
1
vote
2 answers

IComparer did not return zero when Array.Sort called x.CompareTo(x)

I've implemented an IComparer to sort results on a search page. Sometimes, in production, users are getting this error. All the data for the search (criteria, paging, sorting) is on the querystring, and I'm using the ELMAH library, so I can see the…
joelt
  • 2,672
  • 2
  • 24
  • 32
1
vote
1 answer

IComparer compare value of enum

I am implementing a compare method (IComparer) for the game Manille and I need to compare certain cards (enum values) with each other. I want to order them in a certain way so that Number 10 is the biggest card and Ace the second biggest. There is…
Mike L
  • 33
  • 5
1
vote
5 answers

ArrayList Sorting

I have an ArrayList that contains a large number of strings. It needs to be sorted in place based on three fields (essentially three substrings) which are Name, Age and Amt. Age is the first substring (position 0-3), Name is second (3-6) and Amt is…
GonzoKnight
  • 809
  • 7
  • 12
1
vote
3 answers

C#: How to evaluate multipe Comparer classes for an object when checking equality?

Motivation I'm looking to implement IComparer<> in a similar way to the demo code below. Where Foo is the type of objects I need to compare. It does not implement IComparable, but I'm providing an IComparer class for each field so the user can elect…
mbadawi23
  • 1,029
  • 2
  • 21
  • 43
1
vote
1 answer

VB.NET Syntax for instantiating inherited SortedDictionary with Custom Comparer

Here's my starting point: a SortedDictionary with a custom Comparer: Dim dict As SortedDictionary(Of Long, Object) = New SortedDictionary(Of Long, Object)(New CustomComparer()) To implement additional functionality, I need to extend my dictionary…
Len White
  • 892
  • 13
  • 25
1
vote
1 answer

C# List Sort with IComparer argument gives compilation error

Can someone explain to me how this is supposed to work? I followed an MSDN example I found at http://msdn.microsoft.com/en-us/library/234b841s.aspx . I have made my own CustomObject and have made a Comparer for it. Here is the CustomObjectComparer…
Atari2600
  • 2,643
  • 2
  • 20
  • 18
1
vote
2 answers

Sort in alpha order by those that are not prefaced by APPLE and then those that are prefaced with APPLE

I have a grid with following values Arts and Craft Culture Bowling Science and Technology APPLE - Awesome APPLE - Healthy APPLE - Interests APPLE - Nutrients I'm trying to use comparer but not able to get this to work. Please assist. Private…
Brian
  • 35
  • 7
1
vote
1 answer

Why am I getting a null reference exception on my IComparer sort?

I have a list of custom object types say Dim a As New List(Of CustomType) populated with instances. I have a comparer class that inherits Public Class CustomTypeComparer Implements IComparer(Of CustomType) Public Function Compare(x As…
J Collins
  • 2,106
  • 1
  • 23
  • 30
1
vote
2 answers

How to avoid GETDATE() from updating every time table is accessed [SQL Server 2014]

I am using SQL Server 2014. I wish to create a column that automatically captures the time when a record is created and for this value to be persisted. I tried using the GETDATE() function but I learned this could not work since it is not…
chasingD
  • 21
  • 3
1
vote
2 answers

How can I get OrderBy or IComparer to sort my particular list?

The dictionary ints I originally generated randomly within a range between 1 - 100 using: var lstNumbers = Enumerable.Range(1, 100).OrderBy(n => Guid.NewGuid) .GetHashCode()).ToList(); The dictionary is created and used…
1
vote
3 answers

LINQ to Entities - sort by array

I have the following code: public List GetOversizeRegulations(List states) { var tmpList = (from i in _db.States where states.Any(p=>i.Name == p) select new …
Oleg Sh
  • 8,496
  • 17
  • 89
  • 159
1
vote
1 answer

Custom T-SQL Order By (IComparer)

Does T-SQL have any ability to do custom comparison of strings (for sorting), equivalent to .NET's IComparer? Something like the ability to give Order By a user defined function that takes 2 strings and returns a value representing how they compare…
Bradley Uffner
  • 16,641
  • 3
  • 39
  • 76