Let's say I have a string s = "bcabca"
.
What is the simplest way to get "aabbcc"
out of it, i.e., sort the letters in s
?
Let's say I have a string s = "bcabca"
.
What is the simplest way to get "aabbcc"
out of it, i.e., sort the letters in s
?
Revisiting this, my old answer wasn't so good. Here's a better version with
base
functions:Based off this test vector:
Here's a variant of Chase's solution that handles a vector of strings and keeps the original strings as names. ...and I get a chance to promote the use of vapply over sapply :-)
It might be good to mention the
stringi
package for this problem. It'sstri_order
andstri_sort
functions are very efficient, testing at half the time of the base R method mentioned above.Maybe not the most simple answer, but this will work:
Or modify the
strReverse
function that is defined in the help page for?strsplit
to suit our needs. We'll call it strSort: