Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List.Sort in C#: comparer being called with null object

Tags:

I am getting strange behaviour using the built-in C# List.Sort function with a custom comparer.

For some reason it sometimes calls the comparer class's Compare method with a null object as one of the parameters. But if I check the list with the debugger there are no null objects in the collection.

My comparer class looks like this:

public class DelegateToComparer<T> : IComparer<T> {     private readonly Func<T,T,int> _comparer;      public int Compare(T x, T y)     {         return _comparer(x, y);     }      public DelegateToComparer(Func<T, T, int> comparer)     {         _comparer = comparer;     } } 

This allows a delegate to be passed to the List.Sort method, like this:

mylist.Sort(new DelegateToComparer<MyClass>(     (x, y) => {           return x.SomeProp.CompareTo(y.SomeProp);       }); 

So the above delegate will throw a null reference exception for the x parameter, even though no elements of mylist are null.

UPDATE: Yes I am absolutely sure that it is parameter x throwing the null reference exception!

UPDATE: Instead of using the framework's List.Sort method, I tried a custom sort method (i.e. new BubbleSort().Sort(mylist)) and the problem went away. As I suspected, the List.Sort method passes null to the comparer for some reason.

like image 412
cbp Avatar asked Jun 22 '09 08:06

cbp


People also ask

How do you sort a linked list?

Below is a simple insertion sort algorithm for a linked list. 1) Create an empty sorted (or result) list 2) Traverse the given list, do following for every node. ......a) Insert current node in sorted way in sorted or result list. 3) Change head of given linked list to head of sorted (or result) list.

What is sort () and give example?

Sort is a term used to describe the process of organizing data in a particular order allowing for information to be found easier. For example, names and contact information may be sorted in alphabetical order to allow the person looking for a name to see if it's available.

What is bubble sorting in C?

Bubble sort in C is a straightforward sorting algorithm that checks and swaps elements if they are not in the intended order. It compares two adjacent elements to find which one is greater or lesser and switches them based on the given condition until the final place of the element is found.

What is sorting and its types in C?

C language provides five sorting techniques, which are as follows − Bubble sort (or) Exchange Sort. Selection sort. Insertion sort (or) Linear sort. Quick sort (or) Partition exchange sort.


1 Answers

This problem will occur when the comparison function is not consistent, such that x < y does not always imply y < x. In your example, you should check how two instances of the type of SomeProp are being compared.

Here's an example that reproduces the problem. Here, it's caused by the pathological compare function "compareStrings". It's dependent on the initial state of the list: if you change the initial order to "C","B","A", then there is no exception.

I wouldn't call this a bug in the Sort function - it's simply a requirement that the comparison function is consistent.

using System.Collections.Generic;  class Program {     static void Main()     {         var letters = new List<string>{"B","C","A"};          letters.Sort(CompareStrings);     }      private static int CompareStrings(string l, string r)     {         if (l == "B")             return -1;          return l.CompareTo(r);     } } 
like image 165
Lee Avatar answered Oct 08 '22 04:10

Lee