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

C# - custom IComparer that takes an argument as input

I have a list of Points (x,y) that I want to sort by their polar angle with respect to a reference point which I calculate as the average point of all points in the list. How can I set up IComparer so that I can pass the reference point to it for…
0
votes
1 answer

Error on an Icomparer Implementation. Please help !

I created an array sorter to sort arrays like in Win Explorer in a DNN module. As in the answer to a preview question: Sorting an array of folder names like Windows Explorer (Numerically and Alphabetically) - VB.NET A user is getting the following…
Norman
  • 705
  • 1
  • 10
  • 24
0
votes
0 answers

Sort List by reference

I would like to sort a List by reference. Why? I want to compare several lists and find which elements are unique to a given list. Sorting them first in some canonical fashion and then stepping through all lists simultaneously seems a good way to…
Little Endian
  • 784
  • 8
  • 19
0
votes
2 answers

Can I compare objects of different types using NUnit's EqualTo().Using()?

I want to be able to compare any two things that are IConvertible to DateTime using NUnit's Assert.That, preferably without creating a custom constraint. I have a simple IComparer that does the trick nicely. It works with EqualTo().Using(), so long…
Paul Hicks
  • 13,289
  • 5
  • 51
  • 78
0
votes
0 answers

Sorting a list of equal objects with IComparer changes the item ordering

I ran into a problem with IComparer and System.Collections.Generic.List when all items are considered equal: If all items in the source list are considered equal, I would expect the "sorted" list to have exactly the same item order than the source…
Mark
  • 6,647
  • 1
  • 45
  • 88
0
votes
1 answer

Testing a generic IComparer

I'm trying to do a generic comparer and I'm not sure what is going wrong. The comparer's code: namespace Pract_02 { public class ComparerProperty : IComparer { private String attribute; public ComparerProperty(String…
Juan Lopez
  • 361
  • 1
  • 3
  • 16
0
votes
2 answers

ListView.Sort() not working correctly with IComparer C#

Within my code, I have a ListView called allocations which stores the location name and the picking priority as its columns. At the moment, it looks something like this:- | Location | Picking Priority …
Chris Graves
  • 121
  • 1
  • 1
  • 11
0
votes
0 answers

C# custom sort by distance from a point

I am trying to sort a list using IComparer there are about 20,000 items in my list. The first approximately 100 and last 100 items are sorted but the center is not. I am not sure what I am doing wrong. Attached is the parts of the code that does…
kgsw
  • 95
  • 1
  • 10
0
votes
1 answer

Can't remove object from SortedDictionary C#

I have a node class which is contained in a SortedDictionary: SortedDictionary openList = new SortedDictionary(); I need to write the CompareTo method on the node, so that the nodes are sorted from lowest to highest based on…
0
votes
0 answers

Extend object used in WCF service by caller

The WCF web service I'm calling only returns an array of a specific object and IComparer apparently has not been implemented. This makes sorting a little more difficult than I think it should be so I'm wondering if there's a way I can fix that. Is…
NealR
  • 10,189
  • 61
  • 159
  • 299
0
votes
1 answer

Define ICompare (Comparator) in Array.Sort Parameter as in (Java)

How can i convert this java style code into C# ? Here is what i have tried already : I changed the method name from comparable(java) to icompare(c#). Array.Sort(valobject, new IComparer(){ public int Compare(Object obj1, Object obj2)…
Undisputed007
  • 639
  • 1
  • 10
  • 31
0
votes
1 answer

Sorting a SortedDictionary by key length in Visual Basic?

I'm writing a script that anonymizes participant data from a file. Basically, I have: A folder of plaintext participant data (sometimes CSV, sometimes XML, sometimes TXT) A file of known usernames and accompanying anonymous IDs (e.g. jsmith1 as a…
David
  • 303
  • 1
  • 2
  • 8
0
votes
2 answers

Sort List with duplicate keys by custom comparer

I have the List inputColl of type MyValue with many duplicate keys: myValue1.Id=100; myValue2.Id=100; ...etc And I have custom comparer to compare two MyValue elements by Id: inputColl.Sort(myValueComparer); What am I doing wrong? Comparer: public…
Admiral Land
  • 2,304
  • 7
  • 43
  • 81
0
votes
2 answers

Generic Comparer for all objects with a Text property

I am trying to implement a generic comparer (for a sort) for all objects that have a Text property... so two ASP.net textboxes can be compared, two labels or in this specific case two RadTreeNodes in a telerik RadTreeView (as long as they have a…
Craig
  • 849
  • 8
  • 21
0
votes
1 answer

Sorting ListCollectionView with CustomSort

I'm trying to sort a ListCollectionView but I can't get it to work. I have my own CustomSort which throws the following error: Cannot implicitly convert type 'SceneComparer' to 'IComparer'. An explicit conversion exists. (Are you missing a…
Phil
  • 561
  • 2
  • 16
  • 29