Can someone explain to me (as something of a .NET layman) why the Sort()
method of System.Array is static?

Is it because it's changing the array you're sorting? Is there some
limitation where if it was an instance method, it wouldn't be able to
reorder the array?

Just curious!

Cheers,
Matt

Re: Array.Sort? by Marina

Marina
Thu Jan 06 12:45:07 CST 2005

Can't really think of a good reason. Would have made sense to me to make it
an instance method. Unless there is something I'm missing as well.

"mabster" <mhamilton@qafmeats.nospam.com.au> wrote in message
news:ObJkix58EHA.3908@TK2MSFTNGP12.phx.gbl...
> Can someone explain to me (as something of a .NET layman) why the Sort()
> method of System.Array is static?
>
> Is it because it's changing the array you're sorting? Is there some
> limitation where if it was an instance method, it wouldn't be able to
> reorder the array?
>
> Just curious!
>
> Cheers,
> Matt



Re: Array.Sort? by Tim

Tim
Sat Jan 08 02:23:13 CST 2005

Marina wrote:

> Can't really think of a good reason. Would have made sense to me to
> make it an instance method. Unless there is something I'm missing as
> well.

I would imagine that concurrency is the issue. It wouldn't be safe to
sort via an instance method in a thread.

Cheers Tim.


Re: Array.Sort? by Jon

Jon
Sat Jan 08 03:28:16 CST 2005

Tim Jarvis <TimJarvis@newsgroup.nospam> wrote:
> > Can't really think of a good reason. Would have made sense to me to
> > make it an instance method. Unless there is something I'm missing as
> > well.
>
> I would imagine that concurrency is the issue. It wouldn't be safe to
> sort via an instance method in a thread.

There are no particular issues there. The instance itself is still
going to be sorted - what kind of issues would you expect, exactly?
Could you give an example?

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Re: Array.Sort? by Tim

Tim
Sat Jan 08 04:20:36 CST 2005

Jon Skeet [C# MVP] wrote:

> Tim Jarvis <TimJarvis@newsgroup.nospam> wrote:
> > > Can't really think of a good reason. Would have made sense to me
> > > to make it an instance method. Unless there is something I'm
> > > missing as well.
> >
> > I would imagine that concurrency is the issue. It wouldn't be safe
> > to sort via an instance method in a thread.
>
> There are no particular issues there. The instance itself is still
> going to be sorted - what kind of issues would you expect, exactly?
> Could you give an example?

ok, please feel free to completely shoot down my reasoning if it is
incorrect.

I havn't used reflector to have a look at Array.Sort but it strikes me
that if you wanted to make it thread safe, you would have to protect /
lock the instance, and the logical place to do that is in the sort
method itself, but if you were doing that from a instance method, you
would have the possibility of a context switch between the call and the
attempt to protect yourself. Whereas the Static call would not have
that issue, it can protect the instance then manipulate it.

Does that make sense ? of course this makes the assumption that
Array.Sort is in fact thread safe, which maybe its not.

Cheers Tim.

Re: Array.Sort? by Jon

Jon
Sat Jan 08 06:55:57 CST 2005

Tim Jarvis <TimJarvis@newsgroup.nospam> wrote:
> > There are no particular issues there. The instance itself is still
> > going to be sorted - what kind of issues would you expect, exactly?
> > Could you give an example?
>
> ok, please feel free to completely shoot down my reasoning if it is
> incorrect.
>
> I havn't used reflector to have a look at Array.Sort but it strikes me
> that if you wanted to make it thread safe, you would have to protect /
> lock the instance, and the logical place to do that is in the sort
> method itself, but if you were doing that from a instance method, you
> would have the possibility of a context switch between the call and the
> attempt to protect yourself. Whereas the Static call would not have
> that issue, it can protect the instance then manipulate it.

No, it would have exactly the same issues. You can lock the instance
either from an instance method or a static method - but in both cases
other threads could still modify the contents if they didn't know to
also try to lock it.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Re: Array.Sort? by Daniel

Daniel
Sat Jan 08 09:27:06 CST 2005


"mabster" <mhamilton@qafmeats.nospam.com.au> wrote in message
news:ObJkix58EHA.3908@TK2MSFTNGP12.phx.gbl...
> Can someone explain to me (as something of a .NET layman) why the Sort()
> method of System.Array is static?
>
> Is it because it's changing the array you're sorting? Is there some
> limitation where if it was an instance method, it wouldn't be able to
> reorder the array?
>

The core of the reason is that Array.Sort isn't capable of sorting
multidimensional arrays, and they would still have a sort method that does
absolutely nothing. The definitino of a multidimensional array sort is a
pretty tricky beast. Are you sorting rows by their first element? Their
second? Or are you sorting all the elements into a table? Or is it a set of
lists that each need to be sorted?

Sort is static because it does not apply to all arrays equally and has no
reason to be available from any given array. The static method only accepts
single dimension arrays and circumvents that problem.