Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. IComparer trouble.

IComparer trouble.

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpdatabasevisual-studiodata-structureshelp
3 Posts 2 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Martin Cook
    wrote on last edited by
    #1

    Sorry for the long post, but I need some help. I'm writing a class that implements IComparer, and I'm having some problems. What do you make of this: My class looks like this: class RandomComparer : IComparer { private static Random c_random; private static RandomComparer c_instance; public static IComparer Default { get {return c_instance;} } // End Default static RandomComparer() { c_random = new Random(); c_instance = new RandomComparer(); } // End RandomComparer() int IComparer.Compare(object objA, object objB) { // Are the objects different? if (Comparer.Default.Compare(objA, objB) != 0) { // Get two random numbers. int numberA = c_random.Next(0, m_maxSample); int numberB = c_random.Next(0, m_maxSample); // Compare them and return the results. if (numberA > numberB) return 1; else if (numberA < numberB) return -1; } // End if the objects are not the same. return 0; } // End Compare() } // End class RandomCompare I use this class to sort an array like this: string[] someArray = new string[] {"item 0", "item 2", ... "item N"}; Array.Sort(someArray, RandomComparer.Default); Everything works fine most of the time, but every now and then the framework throws an exception that says: "An unhandled exception of type System.ArgumentException occured in mscorlib.dll Additional information: IComparer (or the IComparable methods it relies upon) did not return zero when Array.Sort called x.CompareTo(x). x: value 7 x's type: String The IComparer: RandomComparer" If I set Visual Studio.NET to break for all exceptions, I get a different exception, this time it says: "A first chance exception of type 'System.IndexOutOfRangeException' occured in mscorlib.dll Addition information: Index was outside the bounds of the array." If I elect to continue debugging in this case, the framework then throws the first exception. I searched the Microsoft knowledge base, and did a Google search, but I couldn't find anything. Sheesh! This should be a five minute programming task, but I've worked on this all morning long! :( Has anyone ever run across this? Does anyone have any ideas?

    M D 2 Replies Last reply
    0
    • M Martin Cook

      Sorry for the long post, but I need some help. I'm writing a class that implements IComparer, and I'm having some problems. What do you make of this: My class looks like this: class RandomComparer : IComparer { private static Random c_random; private static RandomComparer c_instance; public static IComparer Default { get {return c_instance;} } // End Default static RandomComparer() { c_random = new Random(); c_instance = new RandomComparer(); } // End RandomComparer() int IComparer.Compare(object objA, object objB) { // Are the objects different? if (Comparer.Default.Compare(objA, objB) != 0) { // Get two random numbers. int numberA = c_random.Next(0, m_maxSample); int numberB = c_random.Next(0, m_maxSample); // Compare them and return the results. if (numberA > numberB) return 1; else if (numberA < numberB) return -1; } // End if the objects are not the same. return 0; } // End Compare() } // End class RandomCompare I use this class to sort an array like this: string[] someArray = new string[] {"item 0", "item 2", ... "item N"}; Array.Sort(someArray, RandomComparer.Default); Everything works fine most of the time, but every now and then the framework throws an exception that says: "An unhandled exception of type System.ArgumentException occured in mscorlib.dll Additional information: IComparer (or the IComparable methods it relies upon) did not return zero when Array.Sort called x.CompareTo(x). x: value 7 x's type: String The IComparer: RandomComparer" If I set Visual Studio.NET to break for all exceptions, I get a different exception, this time it says: "A first chance exception of type 'System.IndexOutOfRangeException' occured in mscorlib.dll Addition information: Index was outside the bounds of the array." If I elect to continue debugging in this case, the framework then throws the first exception. I searched the Microsoft knowledge base, and did a Google search, but I couldn't find anything. Sheesh! This should be a five minute programming task, but I've worked on this all morning long! :( Has anyone ever run across this? Does anyone have any ideas?

      M Offline
      M Offline
      Martin Cook
      wrote on last edited by
      #2

      OK, either my original post was too long for anyone to read or I'm the only person having this problem. Let me simplify things. Has anyone noticed flaky behavior from .NET when creating or using a custom IComparer? I'll probably submit this to Microsoft support, but that will take forever... :( In the meantime, has anyone run across this sort of problem?

      1 Reply Last reply
      0
      • M Martin Cook

        Sorry for the long post, but I need some help. I'm writing a class that implements IComparer, and I'm having some problems. What do you make of this: My class looks like this: class RandomComparer : IComparer { private static Random c_random; private static RandomComparer c_instance; public static IComparer Default { get {return c_instance;} } // End Default static RandomComparer() { c_random = new Random(); c_instance = new RandomComparer(); } // End RandomComparer() int IComparer.Compare(object objA, object objB) { // Are the objects different? if (Comparer.Default.Compare(objA, objB) != 0) { // Get two random numbers. int numberA = c_random.Next(0, m_maxSample); int numberB = c_random.Next(0, m_maxSample); // Compare them and return the results. if (numberA > numberB) return 1; else if (numberA < numberB) return -1; } // End if the objects are not the same. return 0; } // End Compare() } // End class RandomCompare I use this class to sort an array like this: string[] someArray = new string[] {"item 0", "item 2", ... "item N"}; Array.Sort(someArray, RandomComparer.Default); Everything works fine most of the time, but every now and then the framework throws an exception that says: "An unhandled exception of type System.ArgumentException occured in mscorlib.dll Additional information: IComparer (or the IComparable methods it relies upon) did not return zero when Array.Sort called x.CompareTo(x). x: value 7 x's type: String The IComparer: RandomComparer" If I set Visual Studio.NET to break for all exceptions, I get a different exception, this time it says: "A first chance exception of type 'System.IndexOutOfRangeException' occured in mscorlib.dll Addition information: Index was outside the bounds of the array." If I elect to continue debugging in this case, the framework then throws the first exception. I searched the Microsoft knowledge base, and did a Google search, but I couldn't find anything. Sheesh! This should be a five minute programming task, but I've worked on this all morning long! :( Has anyone ever run across this? Does anyone have any ideas?

        D Offline
        D Offline
        Daniel Turini
        wrote on last edited by
        #3

        Hi, Martin. Most sort algorithms need stable comparison functions. Your function seems to be returning random results when the objects are different, so the .NET framework gets lost. Suppose you have created a function where A > B > C > D. Your IComparable should be stable and NEVER return B > A > D > C. Comparison is, too, a transitive function. So, if A > B and B > C, you can safelly assume that A > C. Your function does not guarantees that. My latest article: GBVB - Converting VB.NET code to C#

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups