tidyverse / stringr

A fresh approach to string manipulation in R

Home Page:https://stringr.tidyverse.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

str_sort sorts incorrectly in numeric mode

chrarnold opened this issue · comments

I just encountered the following case with string '1.4.1'

# So far so good
> str_sort(c("0.5", "10", "0.2"), numeric = TRUE)
[1] "0.2" "0.5" "10" 

# Here the confusion comes
> str_sort(c("0.5", "10", "0.25"), numeric = TRUE)
[1] "0.5"  "0.25" "10"  

That's a bug from how I would judge it, or at least I couldnt find any info that str_sort doesnt work properly for strings that are actually numeric.

Here's an actual reprex:

library(stringr)

str_sort(c("0.5", "10", "0.2"), numeric = TRUE)
#> [1] "0.2" "0.5" "10"
str_sort(c("0.5", "10", "0.25"), numeric = TRUE)
#> [1] "0.5"  "0.25" "10"

Created on 2023-01-17 with reprex v2.0.2

The problem is that it only affects digits, not decimals. I don't think there's anything we can do about that as it's a feature of the underlying ICU library.

I think this limitation might be worth mentioning somewhere