icomparer

When will a Comparer make Sort throw an ArgumentException?

The documentation for Sort says that Sort will throw an ArgumentException if "The implementation of comparer caused an error during the sort. For example, comparer might not return 0 when comparing an item with itself." Apart from the example given, can anyone tell me when this would otherwise happen? ...

IComparer problem + How do I sort an array of strings naturally (FILE_10 > FILE_2) in .NET?

SOLVED at the bottom of my post. Or more specifically: I have a bunch of FileInfo objects (I need the FileInfo objects to exclude hidden, system and reparse point files). I need to sort FileInfo[] naturally based on their FileInfo.FullName. So FILE_10.ext should come after FILE_2.ext. Luckily the FileInfo[] contains files of only one ...

When to use IComparable<T> Vs. IComparer<T>

I'm trying to figure out which of these interfaces I need to implement. They both essentially do the same thing. When would I use one over the other? ...

Shuffle using IComparer

First of all, I do know about the Fisher-Yates shuffle. But lets say for arguments sake that I want to allow the user to pick a sort option from a Dropdown list. This list would include a "Random" option. Based on the result of their selection I just want to substitute in an IComparer instance for my sort. What would the IComparer lo...

SortedList not sorting on key - VB.NET

Hello 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 -------------------------------- 1 "700-800" | List(Of Object) 2 "900-1000" | List(Of...

List.Sort IComparer performance

I'm trying to sort a pair of int arrays (int[] a; int[] b;) If I use Array.Sort(a,b) then the performance is great. However, I'd prefer to use a List<> and load the int pairs in a struct. I can get this to work using Array.Sort() with an overload that provides a simple comparer for the struct but it's about 4 times slower than the Arra...

Use own IComparer<T> with Linq OrderBy

I have a generic List<MyClass> where MyClass has a property "InvoiceNumber" which contains values like: 200906/1 200906/2 .. 200906/10 200906/11 200906/12 My list is bound to a BindingList<T> which supports sorting with linq: protected override void ApplySortCore( PropertyDescriptor property, ListSortDirection directi...

C# modify List.Contains behavior

Hi, I have a List<MyObj> with the class MyObj : IComparable. I wrote the method CompareTo in the MyObj class per the IComparable interface, but when I use the List<MyObj>.Contains(myObjInstance) it returns false when it should be true. I'm not sure I'm understanding how I need to proceed to make sure the List uses my custom comparison...

Problem with custom IComparer for List (sort) - c#

Hi, can anyone help, i have problem doing a sort, I thought i had it sorted but appears not to be working. I have a List which stores the following values 8,6,10,11,7 I also have another List (accessories in my class and it has a propert called accessoryId current the classes are in the order of id which is currenty 6,7,8,10,11) H...

Is there a reasonable scenario for a stateful IComparer<T>?

I have never written a stateful IComparer<T> with a default constructor. All standard library implementations which I've checked in Reflector are stateless as well. Therefore, I'd like to assume that I can freely cache IComparer<T> like this: PriorityQueue<TPriority, TComparer> where TComparer : IComparer<TPriority>, new() { private...

Sort String Array As Int

Is there some way to use IComparer with ArrayList.Sort() to sort a group of strings as ints? ...

"Cannot convert to IComparer"

I have the following IComparer defined for boxed RegistryItem objects: public class BoxedRegistryItemComparer : IComparer<object> { public int Compare(object left, object right) { RegistryItem leftReg = (RegistryItem)left; RegistryItem rightReg = (RegistryItem)right; return string.Compare(leftReg.Name, r...

Reverse-sorting a Listview (with Comparer class)

I have a two-column ListView linked to a Datapager. The ListView lists all files of a particular type in a particular directory, so the data source is an ArrayList of type FileInfo. Consequently, I had to create a Comparer class to use in the ArrayList.Sort method. FileInfoComparer.vb: Imports System.IO Friend Class FileInfoDateCompar...

What is wrong with this Custom Compare function

I was trying to debug a problem and ran into this issue. Maybe somebody can explain it to me. This is the code in question public int Compare(CustomClass rt1, CustomClass rt2) { if (rt1 == null if (rt1 == null) return -1; if (rt2 == null) return 1; if (rt1.yPos < rt2.yPos) ...

Using OrderBy with custom IComparer with SubSonic

I am trying to call OrderBy() using a custom IComparer on a SubSonic IQueryable like so: IQueryable<FooObject> sortedFoos = FooObject.All() .OrderBy(f => f, new FooObjectComparer()); However when I then try to enumerate over sortedFoos or create a PagedList<FooObject> using it, I get a System.Exception: 'The LINQ expression nod...

WPF - Change the comparer for a ListBox

I have a list box that contains a list of WorkItems (from TFS). I want to add items to be selected, but it is failing to identify the items as the same. Here is my code public void SelectQueryResultItem(WorkItem item) { lstQueryResults.SelectedItems.Add(item); } This works great when the WorkItem passed in is fro...

Sorting a Listbox by value

I have a list box that I need to sort by the values not the text. I'm very new to this so please excuse my ignorance. I started looking at IComparer but I'm some what lost in how to use it. How can I overload my list box with this new sort? Thank You ...

using Comparer to sort IEnumerable in C# by different fields

I have a list of an object which need to be sorted depending on three different properties of the object. Example CLass Object1{ Property1 , Property2, Property3} ListObj = IEnumerable<Object1> Foreach ( item in ListObj){ if (item.Property1 == true) item goes at top of list if(item.Property2 == true) item goes e...

Effectively disable Sort() within a CompareTo() override?

The CompareTo() method for my class is dynamic, and can range from a simple comparison to comparisons on a number of columns. This is all determined at run time, and it works great. But in some cases, I want any attempt to sort a collection of my objects using the default comparison to just do nothing. Having CompareTo() just return a ...

Optimizing a bin-placement algorithm

Alright, I've got two collections, and I need to place elements from collection1 into the bins (elements) of collection2, based on whether their value falls within a given bin's range. For a concrete example, assume I have a sorted collection objects (bins) which have an int range ([1...4], [5..10], etc). I need to determine the range a...