简体   繁体   中英

How is ording a List of objects handled with Linq and or Sort?

Sorry if this is a duplicate. I'm having a hard time finding an answer because I get so much bloat of people asking how to sort lists in general in my search results.

Say I have a property like this:

public List<object> Hello { get; set; }

Say each element in the list is of the same nullable primitive data type.

So every element is a int? or every element is a decimal? or every element is a DateTime?, etc

What exactly happens if I invoke a Sort or OrderBy on this list? Does it sort based on the underlying type of each element or does it order by the string value of the property?

Let's check http://ideone.com/kcXmHE :

System.ArgumentException: Value does not fall within the expected range.
  at System.String.CompareTo (System.Object value) [0x00000] in <filename unknown>:0 
  at System.Collections.Generic.Comparer`1+DefaultComparer[System.Object].Compare (System.Object x, System.Object y) [0x00000] in <filename unknown>:0 
  at System.Linq.SortSequenceContext`2[System.Object,System.Object].Compare (Int32 first_index, Int32 second_index) [0x00000] in <filename unknown>:0 
  at System.Array.qsort[Int32] (System.Int32[] keys, Int32 low0, Int32 high0, IComparer`1 comparer) [0x00000] in <filename unknown>:0 
  at System.Array.SortImpl[Int32] (System.Int32[] keys, Int32 index, Int32 length, IComparer`1 comparer) [0x00000] in <filename unknown>:0 
  at System.Array.Sort[Int32] (System.Int32[] array, IComparer`1 comparer) [0x00000] in <filename unknown>:0 
  at System.Linq.QuickSort`1[System.Object].PerformSort () [0x00000] in <filename unknown>:0 
  at System.Linq.QuickSort`1+<Sort>c__Iterator0[System.Object].MoveNext () [0x00000] in <filename unknown>:0 
  at System.Collections.Generic.List`1[System.Object]..ctor (IEnumerable`1 collection) [0x00000] in <filename unknown>:0 
  at System.Linq.Enumerable.ToList[Object] (IEnumerable`1 source) [0x00000] in <filename unknown>:0 
  at Test.Main () [0x00000] in <filename unknown>:0 

So, LINQ uses default comparer for element of list (for string value you can see System.String.CompareTo ) and tries to compare with another value (of object type). And, can't do it.

If you change order of elements in initial list ( http://ideone.com/foljli ), you will get another stack trace:

System.ArgumentException: Object must be of type Int32.
  at System.Int32.CompareTo (System.Object value) [0x00000] in <filename unknown>:0 
  at System.Collections.Generic.Comparer`1+DefaultComparer[System.Object].Compare (System.Object x, System.Object y) [0x00000] in <filename unknown>:0 
  at System.Linq.SortSequenceContext`2[System.Object,System.Object].Compare (Int32 first_index, Int32 second_index) [0x00000] in <filename unknown>:0 
  at System.Array.qsort[Int32] (System.Int32[] keys, Int32 low0, Int32 high0, IComparer`1 comparer) [0x00000] in <filename unknown>:0 
  at System.Array.SortImpl[Int32] (System.Int32[] keys, Int32 index, Int32 length, IComparer`1 comparer) [0x00000] in <filename unknown>:0 
  at System.Array.Sort[Int32] (System.Int32[] array, IComparer`1 comparer) [0x00000] in <filename unknown>:0 
  at System.Linq.QuickSort`1[System.Object].PerformSort () [0x00000] in <filename unknown>:0 
  at System.Linq.QuickSort`1+<Sort>c__Iterator0[System.Object].MoveNext () [0x00000] in <filename unknown>:0 
  at System.Collections.Generic.List`1[System.Object]..ctor (IEnumerable`1 collection) [0x00000] in <filename unknown>:0 
  at System.Linq.Enumerable.ToList[Object] (IEnumerable`1 source) [0x00000] in <filename unknown>:0 
  at Test.Main () [0x00000] in <filename unknown>:0 

Now, LINQ takes int32 and tries to compare with object. And fail again. Because int32 type has no method CompareTo(object)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM