Note that mysort2 is slightly more general as it handles the case
that the strings begin with numerics:
> u <- c("51a2", "2a4")
> mysort(u)
[1] "51a2" "2a4"
> mysort2(u)
[1] "2a4" "51a2"
On Mon, Dec 22, 2008 at 12:32 AM, John Fox wrote:
> Dear Gabor,
>
> Thank you (again) for this second sugges
Dear Gabor,
Thank you (again) for this second suggestion, which does exactly what I
want. At the risk of appearing ungrateful, and although the judgment is
admittedly subjective, I don't find it simpler than mysort().
For curiosity, I tried some timings of the two functions for the sample
problem
Another possibility is to use strapply in gsubfn giving a solution
that is non-recursive and shorter:
library(gsubfn)
mysort2 <- function(s) {
L <- strapply(s, "([0-9]+)|([^0-9]+)",
~ if (nchar(x)) sprintf("%9d", as.numeric(x)) else y)
L2 <- t(do.call(cbind, lapply
Dear Gabor,
Thanks for this -- I was unaware of mixedsort(). As you point out,
however, mixedsort() doesn't cover all of the cases in which I'm
interested and which are handled by mysort().
Regards,
John
On Sun, 21 Dec 2008 20:51:17 -0500
"Gabor Grothendieck" wrote:
> mixedsort in gtools will
mixedsort in gtools will give the same result as mysort(s) but
differs in the case of t.
On Sun, Dec 21, 2008 at 8:33 PM, John Fox wrote:
> Dear r-helpers,
>
> I'm looking for a way of sorting variable names in a "natural" order, when
> the names are composed of digits and other characters. I kno
5 matches
Mail list logo