IComparer and Sort

  • Thread starter Thread starter coder316
  • Start date Start date
C

coder316

Hello,
I am trying to find (and understand) the connection between CompareTo
and Sort. When I call Sort, it calls my implementation of CompareTo,
but I dont see a connection in the object browser.
Thanks
 
I am trying to find (and understand) the connection between CompareTo
and Sort. When I call Sort

....on what?
it calls my implementation of CompareTo,
but I dont see a connection in the object browser.

The "connection" is in the code of whatever object you're calling Sort() on.
 
...on what?


The "connection" is in the code of whatever object you're calling Sort() on.

When I call this:
Array.Sort(blah);
This gets run:
public int CompareTo(object obj) // Implement the method.
{
MyClass mc = (MyClass)obj;
if (this.TheValue < mc.TheValue) return -1;
if (this.TheValue > mc.TheValue) return 1;
return 0;
}
I know theres a heirarchy of calls being made, but I cant find hem.
Anybody with an Explanation?
 
When I call this:
Array.Sort(blah);
This gets run:
public int CompareTo(object obj) // Implement the method.
{
MyClass mc = (MyClass)obj;
if (this.TheValue < mc.TheValue) return -1;
if (this.TheValue > mc.TheValue) return 1;
return 0;
}
I know theres a heirarchy of calls being made, but I cant find hem.
Anybody with an Explanation?

http://www.red-gate.com/products/reflector/
 

I know your trying to help me find it myself, and for most things,
that works,but a good explanation ala Peter Duniho or Nicholas Paldino
would go along way.
Sometimes there is just one little link missing in our understanding
of things.
I know about IComparer and Array.Sort and interfaces in general, but
this one roundabout way of going from Sort to CompareTo is slipping
through.
Thanks


Thanks
 
I know your trying to help me find it myself, and for most things,
that works,but a good explanation ala Peter Duniho or Nicholas Paldino
would go along way.
Sometimes there is just one little link missing in our understanding
of things.
I know about IComparer and Array.Sort and interfaces in general, but
this one roundabout way of going from Sort to CompareTo is slipping
through.
Thanks

Thanks- Hide quoted text -

- Show quoted text -

By the way Jeff,
Thats a great piece of software.
Thanks
 
I know your trying to help me find it myself, and for most things,
that works,but a good explanation ala Peter Duniho or Nicholas Paldino
would go along way.

I guess that's why I'm an ex-MVP and they're still current!

Seriously, I fired up Reflector hoping to post the code that called
CompareTo(), but it ended up being so complex that I decided I just didn't
feel like digging for it and that if you wanted to know so bad you'd find it
for yourself. Besides, using Reflector is an absolutely excellent way of
learning about the .NET framework. It's almost like using the Web: you start
by looking for one specific you find yourself "hyperlinking" through loads
of other functions and then you realize how much of your life you just
burned away. Don't say I didn't warn you....
 
coder316 said:
I know your trying to help me find it myself, and for most things,
that works,but a good explanation ala Peter Duniho or Nicholas Paldino
would go along way.
Sometimes there is just one little link missing in our understanding
of things.
I know about IComparer and Array.Sort and interfaces in general, but
this one roundabout way of going from Sort to CompareTo is slipping
through.

Naturally the object browser isn't showing you what you're looking for.
It doesn't show you what methods DO with the objects supplied as call
arguments or that are member data of the objects to which the methods
belonging. It doesn't show you which methods a method calls.

Array knows what its elements are. If the elements implement IComparer,
the array's Sort method is able to call the elements' CompareTo methods,
so it can compare them to each other and sort them. Does that cover what
you weren't understanding, or is there anything else?
 
Jeff Johnson said:
I guess that's why I'm an ex-MVP and they're still current!

Seriously, I fired up Reflector hoping to post the code that called
CompareTo(), but it ended up being so complex that I decided I just didn't
feel like digging for it and that if you wanted to know so bad you'd find
it for yourself. Besides, using Reflector is an absolutely excellent way
of learning about the .NET framework. It's almost like using the Web: you
start by looking for one specific

thing and then
you find yourself "hyperlinking" through loads of other functions and then
you realize how much of your life you just burned away. Don't say I didn't
warn you....

(Dropped a few words there....)
 
Here I have a copy from the call stack that show something about the call
sequence.
What I don't understand here is how a call to Compare can call CompareTo

ConsoleApplication7.exe!Person.CompareTo(object obj = {Person}) Line 32 C#
mscorlib.dll!System.Collections.Comparer.Compare(object a, object b) + 0x5f
bytes
mscorlib.dll!System.Array.SorterObjectArray.SwapIfGreaterWithItems(int a =
0, int b = 2) + 0x59 bytes
mscorlib.dll!System.Array.SorterObjectArray.QuickSort(int left = 0, int
right = 4) + 0x40 bytes
mscorlib.dll!System.Array.Sort(System.Array keys, System.Array items, int
index, int length, System.Collections.IComparer comparer) + 0x117 bytes
mscorlib.dll!System.Collections.ArrayList.Sort(int index, int count,
System.Collections.IComparer comparer) + 0x38 bytes
mscorlib.dll!System.Collections.ArrayList.Sort() + 0x2f bytes
ConsoleApplication7.exe!Program.Main(string[] args = {Dimensions:[0]}) Line
16 + 0xb bytes

//Tony
 
Here I have a copy from the call stack that show something about the call
sequence.
What I don't understand here is how a call to Compare can call CompareTo

ConsoleApplication7.exe!Person.CompareTo(object obj = {Person}) Line 32C#
mscorlib.dll!System.Collections.Comparer.Compare(object a, object b) + 0x5f
bytes
mscorlib.dll!System.Array.SorterObjectArray.SwapIfGreaterWithItems(int a =
0, int b = 2) + 0x59 bytes
mscorlib.dll!System.Array.SorterObjectArray.QuickSort(int left = 0, int
right = 4) + 0x40 bytes
mscorlib.dll!System.Array.Sort(System.Array keys, System.Array items, int
index, int length, System.Collections.IComparer comparer) + 0x117 bytes
mscorlib.dll!System.Collections.ArrayList.Sort(int index, int count,
System.Collections.IComparer comparer) + 0x38 bytes
mscorlib.dll!System.Collections.ArrayList.Sort() + 0x2f bytes
ConsoleApplication7.exe!Program.Main(string[] args = {Dimensions:[0]}) Line
16 + 0xb bytes

//Tony

"Harlan Messinger" <[email protected]> skrev i meddelandet

Naturally the object browser isn't showing you what you're looking for.It
doesn't show you what methods DO with the objects supplied as call
arguments or that are member data of the objects to which the methods
belonging. It doesn't show you which methods a method calls.
Array knows what its elements are. If the elements implement IComparer,
the array's Sort method is able to call the elements' CompareTo methods,
so it can compare them to each other and sort them. Does that cover what
you weren't understanding, or is there anything else?- Hide quoted text-

- Show quoted text -

Jeff,
Interesting.. I didnt see the missing words, but people have the
ability of filling in missing words.
Theres a little paragraph out there where all the words are spelled
completely wrong but you could still read it.
Go Figure.
Harlan,
Thanks Also. I'm starting to "get it"
 
coder316 said:
When I call this:
Array.Sort(blah);
This gets run:
public int CompareTo(object obj) // Implement the method.
{
MyClass mc = (MyClass)obj;
if (this.TheValue < mc.TheValue) return -1;
if (this.TheValue > mc.TheValue) return 1;
return 0;
}
I know theres a heirarchy of calls being made, but I cant find hem.
Anybody with an Explanation?

It's difficult to provide an exact explanation without knowing exactly
what your experience level is, and what your specific question is. But,
making some assumptions:

-- When you call Array.Sort(), that method has to do comparisons
between the elements of the array

-- One way to define a comparison between objects is to implement
IComparable

-- Through the magic of polymorphism, each time Array.Sort() wants
to know whether a given object is "less than", "greater than", or "equal
to" another given object, and those objects implement IComparable,
Array.Sort() can just cast the object reference to IComparable and call
the CompareTo() method in that interface.

Depending on how much you know about sorting algorithms and
polymorphism, the above may or may not make sense. Also depending on
what your actual question is, the above may or may not actually address
what you're asking.

I'm also not clear on why you are asking about the object browser. If
you're looking for "a hierarchy of calls", that's a run-time thing that
you can look at in the debugger. But the object browser doesn't know
anything about that. It just shows you the members of classes and the
relationships between class _declarations_, not run-time relationships
related to the execution of code.

If the above doesn't answer the question, you should consider trying to
be more specific, rephrasing the question so it's more clear exactly
what you don't understand.

Pete
 
Tony said:
Here I have a copy from the call stack that show something about the call
sequence.
What I don't understand here is how a call to Compare can call CompareTo

int Compare(object a, object b)
{
return ((IComparable)a).CompareTo(b);
}

Obviously, "a" has to implement IComparable for that to work.

Pete
 
Hello!

At the end is a complete program that give the call stack shown below
I use Red Gate's .NET Reflector on my assembly.
I can click in the disassembler and follow the sequence of calls that is
being made up to this call to QuickSort
mscorlib.dll!System.Array.SorterObjectArray.QuickSort(int left = 0, int
right = 4) + 0x40 bytes
I can't understand how this call to SwapIfGreaterWithItems can be made from
the QuickSort ?
And as the last question I have tried to find the call to CompareTo using
Red Gate's .NET Reflector on my assembly but
I can't find it
int Compare(object a, object b)
{
return ((IComparable)a).CompareTo(b);
}

I just wonder if some code is hidden in some way so I have not chance to see
it that
would explain why I can't find the call to CompareTo


ConsoleApplication7.exe!Person.CompareTo(object obj = {Person}) Line 34 C#
mscorlib.dll!System.Collections.Comparer.Compare(object a, object b) + 0x5f
bytes
mscorlib.dll!System.Array.SorterObjectArray.SwapIfGreaterWithItems(int a =
0, int b = 2) + 0x59 bytes
mscorlib.dll!System.Array.SorterObjectArray.QuickSort(int left = 0, int
right = 4) + 0x40 bytes
mscorlib.dll!System.Array.Sort(System.Array keys, System.Array items, int
index, int length, System.Collections.IComparer comparer) + 0x117 bytes
mscorlib.dll!System.Collections.ArrayList.Sort(int index, int count,
System.Collections.IComparer comparer) + 0x38 bytes
mscorlib.dll!System.Collections.ArrayList.Sort() + 0x2f bytes
ConsoleApplication7.exe!Program.Main(string[] args = {Dimensions:[0]}) Line
16 + 0xb bytes C#

class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
list.Add(new Person("hugo",34));
list.Add(new Person("stina",12));
list.Add(new Person("kalle",43));
list.Add(new Person("hugo",11));
list.Add(new Person("bob",17));
list.Sort();
}
}

class Person : IComparable
{
private string name;
private int age;

public Person(string name, int age)
{
this.name = name;
this.age = age;
}
public int CompareTo(object obj)
{
if (obj is Person)
{
Person person = (Person)obj;
return this.age - person.age;
}
else
throw new ArgumentException("Illegal object");
}
}

//Tony
 
     int Compare(object a, object b)
     {
         return ((IComparable)a).CompareTo(b);
     }

Obviously, "a" has to implement IComparable for that to work.

Pete

Pete,
Thanks. I think whats holding me up is that there is no direct call to
CompareTo from anywhere, or am I missing something.
Is it because it's the only Method of IComparable?
 
Pete,
Thanks. I think whats holding me up is that there is no direct call to
CompareTo from anywhere, or am I missing something.
Is it because it's the only Method of IComparable?

In the following code I am declaring and useing an interface.Its
pretty strait forward.This is my understanding of how it works.
But as you can see, I'm calling AddNumbers. I cant see the roundabout
way it's getting done in CompareTo.
Thats what I'm having a problem with.
namespace Interface2
{
interface MyInterface
{
int AddNumbers(int x, int y);
}
class Program
{
static void Main(string[] args)
{
UseInterface useinterface = new UseInterface();
Console.WriteLine(useinterface.AddNumbers(2,2));
Console.ReadLine();
}
}
class UseInterface:MyInterface
{

public int AddNumbers(int a, int b)
{
int c;
return c = a + b;
}

}
}
 
coder316 said:
Pete,
Thanks. I think whats holding me up is that there is no direct call to
CompareTo from anywhere, or am I missing something.

There's no such thing as a call that isn't a *direct* call by *something*.
Is it because it's the only Method of IComparable?

It's because Compare calls it, as Pete showed you. And Sort calls
Compare, directly or indirectly.
 
There's no such thing as a call that isn't a *direct* call by *something*..


It's because Compare calls it, as Pete showed you. And Sort calls
Compare, directly or indirectly.- Hide quoted text -

- Show quoted text -

I found this on msdn:
The instance's IComparable implementation is called AUTOMATTICALLY by
methods such as Array.Sort
These calls are going on behind the scenes and as Jeff mentioned I
could walk my way through it in the Disassembler, taking several steps
(as the call stack shows) to get from Sort to CompareTo.
Correct?
 
coder316 said:
I found this on msdn:
The instance's IComparable implementation is called AUTOMATTICALLY by
methods such as Array.Sort
These calls are going on behind the scenes and as Jeff mentioned I
could walk my way through it in the Disassembler, taking several steps
(as the call stack shows) to get from Sort to CompareTo.
Correct?
You seem to be trying to draw distinctions that don't exist when you
write "automatically" and "behind the scenes". The code a developer
writes will generally call method provided in one or more libraries.
Everything the called library methods do is "automatic", and all of it
is "behind the scenes" insofar as the code inside these library-supplied
methods is invisible to you. This is true whether you call Console.Write
or Encoding.ToString or Form.Show or any other library-supplied method
or property.
 
coder316 said:
[...]
int Compare(object a, object b)
{
return ((IComparable)a).CompareTo(b);
}

Thanks. I think whats holding me up is that there is no direct call to
CompareTo from anywhere, or am I missing something.

I don't understand the question. My code shows what I'd call a "direct
call to CompareTo()".
Is it because it's the only Method of IComparable?

No. IComparable could have as many methods as you want. It's the fact
that in the code example I wrote, I typed the characters "CompareTo" as
the method to call.

I don't know how much more "direct" it could be. :)

Pete
 
Back
Top