none
mscorlib Sort ERROR RRS feed

  • Question

  • The following ERROR occurs win trying to run the following code as it appears in the Pro C# 2010 and the .NET 4 Platform 5th Edition from Apress on page 357. I have the .NET 4.5 installed in Windows 7 Ult. Version. Is this the wrong place to be asking this question? Should I be looking at the Errata for the book? Has the "Sort" Method been mover to another Library?

    Array.Sort(myAutos);

    Below is the InvalidOperationException Details ->{"Failed to compare two elements in the array."}

    Why is this error occuring? Do I have a corrupt file and if this is the case where can I get a new file?

    System.InvalidOperationException was unhandled
      HResult=-2146233079
      Message=Failed to compare two elements in the array.
      Source=mscorlib
      StackTrace:
           at System.Collections.Generic.ArraySortHelper`1.Sort(T[] keys, Int32 index, Int32 length, IComparer`1 comparer)
           at System.Array.Sort[T](T[] array, Int32 index, Int32 length, IComparer`1 comparer)
           at System.Array.Sort[T](T[] array)
           at ComparableCar.Program.Main(String[] args) in C:\Users\Mark\documents\visual studio 2010\Projects\ComparableCar\ComparableCar\Program.cs:line 29
           at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException: System.ArgumentException
           HResult=-2147024809
           Message=At least one object must implement IComparable.
           Source=mscorlib
           StackTrace:
                at System.Collections.Comparer.Compare(Object a, Object b)
                at System.Collections.Generic.ObjectComparer`1.Compare(T x, T y)
                at System.Collections.Generic.ArraySortHelper`1.SwapIfGreater(T[] keys, IComparer`1 comparer, Int32 a, Int32 b)
                at System.Collections.Generic.ArraySortHelper`1.DepthLimitedQuickSort(T[] keys, Int32 left, Int32 right, IComparer`1 comparer, Int32 depthLimit)
                at System.Collections.Generic.ArraySortHelper`1.Sort(T[] keys, Int32 index, Int32 length, IComparer`1 comparer)
           InnerException:

    Sunday, May 5, 2013 12:49 AM

Answers

  • I have the 2008 version of the on-line book and found the example on page 317.  Below it says in th ebook you r will get exactly the error you  posted.  It is possible in your addition the publisher left out the warning.

    // Sort my cars?

    Array.Sort(myAutos);

    If you run this test, you would find that an

    ArgumentException exception is thrown by the runtime,

    "At least one object must implement IComparable."

    300

    CHAPTER 9 n WORKING WITH INTERFACES

    with the following message:


    jdweng


    Sunday, May 5, 2013 3:18 AM

All replies

  • I have the 2008 version of the on-line book and found the example on page 317.  Below it says in th ebook you r will get exactly the error you  posted.  It is possible in your addition the publisher left out the warning.

    // Sort my cars?

    Array.Sort(myAutos);

    If you run this test, you would find that an

    ArgumentException exception is thrown by the runtime,

    "At least one object must implement IComparable."

    300

    CHAPTER 9 n WORKING WITH INTERFACES

    with the following message:


    jdweng


    Sunday, May 5, 2013 3:18 AM
  • Thank you for this answer. I downloaded the source and opened the project and compiled and ran it without error. The only difference in the code was that the Class was declared in the Program.cs instead of in and Added class.

    I have noticed similar error in working with VS2010. At times it seems that Visual Studio has a mind of its own. Sometimes I can Save, Close and ReOpen the same project and the Errors are visible for a moment and then disappear as if by magic..

    Any thoughts on this matter?

    Mark

    Wednesday, May 8, 2013 5:00 PM
  • I always to a recompile from the menu after making changes.  I often find the the error screen doesn't accurately catch all the corrections until a full compile is performed.

    jdweng

    Wednesday, May 8, 2013 5:12 PM