From maechler at stat.math.ethz.ch  Wed Jun  7 17:30:48 2006
From: maechler at stat.math.ethz.ch (Martin Maechler)
Date: Wed, 7 Jun 2006 17:30:48 +0200
Subject: [Rd] sort() generic? [Re: Suspicious behaviour of sort on POSIXct
	..]
In-Reply-To: <Pine.LNX.4.64.0606062109200.4685@gannet.stats.ox.ac.uk>
References: <OF572FD044.0131BC92-ON80257185.005D8210-80257185.005E5FD4@bnpparibas.com>
	<Pine.LNX.4.64.0606062109200.4685@gannet.stats.ox.ac.uk>
Message-ID: <17542.61736.871305.720154@stat.math.ethz.ch>

{Diverted to R-devel}

>>>>> "BDR" == Prof Brian Ripley <ripley at stats.ox.ac.uk>
>>>>>     on Tue, 6 Jun 2006 21:14:01 +0100 (BST) writes:

    BDR> On Tue, 6 Jun 2006, patrick.guevel at uk.bnpparibas.com
    BDR> wrote:
    >> Hi ,
    >> 
    >> When I sort a vector of POSIXct values in R-2.3.0 and
    >> R-2.3.1, I get a vector of numeric values and this gets
    >> some of my code to crash (class object creation). Is that
    >> a R bug?

    BDR> No, it is as documented: see ?sort

    BDR>       As from R 2.3.0, all attributes are removed from
    BDR> the return value except names, which are sorted.  (If
    BDR> 'partial' is specified even the names are removed.)

    BDR> Note, the class is an attribute.  For many classes
    BDR> sorting destroys the appropriateness of the class.

Indeed, and I agree this a good change.
However, the above also suggests that ideally,  sort() would be
a generic function.

One good reason for  sort()  not being generic now is the fact
that method dispatch costs a bit, *and* that we like sort() to
be really fast.
One way to achieve a generic sort() and keep the possibility of
of very fast sort() --- similarly to  rep() and rep.int()
would be to rename the current sort into something like
sortNum() {"Num" for numeric}, make sort() into a generic,
and replace sort() by sortNum() in those code parts which need
to remain optimally fast.


    >> In the mean time. I'll try to override the sort function
    >> for the POSIXct objects.

    BDR> You can restore the class if appropriate.



[Rd] sort() generic? [Re: Suspicious behaviour of sort on POSIXct ..]
Martin Maechler