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

Martin Maechler maechler at stat.math.ethz.ch
Wed Jun 7 17:30:48 CEST 2006


{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.



More information about the R-devel mailing list