I have character vector of the following shape:
fld <- c('20*20', '100*100', '200*200', '50*50', '1000*1000', '250*250')
I need to sort elements according to a value of number before the star.
sort(fld)
gives:
[1] "100*100" "1000*1000" "20*20" "200*200" "250*250" "50*50"
instead of desirable:
[1] "20*20" "50*50" "100*100" "200*200" "250*250" "1000*1000"
I've prepared the following expression which does thing right:
fld[
charmatch(
paste(
as.character(sort(as.integer(
gsub('\\*.{2,4}', '', fld)
))),
'*', sep = ''
),
fld)
]
but I bet that there is shorter / easier / more natural way...