Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort strings with integers by numeric ordering?

Tags:

regex

sorting

r

I have a column in a data.table full of strings in the format string+integer. e.g.

string1, string2, string3, string4, string5,

When I use sort(), I put these strings in the wrong order.

string1, string10, string11, string12, string13, ..., string2, string20, 
string21, string22, string23, ....

How would I sort these to be in the order

string01, string02, string03, string04, strin0g5, ... , string10,, string11, 
string12, etc.   

One method could be to add a 0 to each integer <10, 1-9? I suspect you would extract the string with str_extract(dt$string_column, "[a-z]+") and then add a 0 to each single-digit integer...somehow with sprintf()

like image 599
ShanZhengYang Avatar asked Jan 22 '26 09:01

ShanZhengYang


2 Answers

We can remove the characters that are not numbers to do the sorting

dt[order(as.integer(gsub("\\D+", "", col1)))]
like image 137
akrun Avatar answered Jan 25 '26 09:01

akrun


You could go for mixedsort in gtools:

vec <- c("string1", "string10", "string11", "string12", "string13","string2", 
         "string20", "string21", "string22", "string23")

library(gtools)
mixedsort(vec)

#[1] "string1"  "string2"  "string10" "string11" "string12" "string13"
# "string20" "string21" "string22" "string23"
like image 28
989 Avatar answered Jan 25 '26 08:01

989



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!