crestor
crestor

Reputation: 1486

change collation priority for accented letters

Faced with the need to imitate the behavior of an old system (from the mainframe era), I need to program an specific collation criteria where the non-ASCII letters get the least priority.

I have started writing something like this (works only for the first letter of the string):

library(tidyverse)
library(stringi)

df <- tribble(
  ~nombre,
  "Alonso",
  "Álvarez",
  "Zapatero"
)
df %>%
  arrange(nombre)
#> # A tibble: 3 x 1
#>   nombre  
#>   <chr>   
#> 1 Alonso  
#> 2 Álvarez 
#> 3 Zapatero
df %>%
  arrange(stri_trans_general(str_sub(nombre, 1, 1), "Latin-ASCII") != str_sub(nombre, 1, 1),
          nombre)
#> # A tibble: 3 x 1
#>   nombre  
#>   <chr>   
#> 1 Alonso  
#> 2 Zapatero
#> 3 Álvarez

Would you suggest some alternative approachs?

Upvotes: 0

Views: 79

Answers (1)

crestor
crestor

Reputation: 1486

I've just found the answer: using icuSetCollate(locale = "ASCII")

library(tidyverse)
library(stringi)
df <- tribble(
  ~nombre,
  "Alonso",
  "Álvarez",
  "Zapatero"
)

icuSetCollate(locale = "ASCII")
df %>%
  arrange(nombre)
#> # A tibble: 3 x 1
#>   nombre  
#>   <chr>   
#> 1 Alonso  
#> 2 Zapatero
#> 3 Álvarez

icuSetCollate(locale = "default")
df %>%
  arrange(nombre)
#> # A tibble: 3 x 1
#>   nombre  
#>   <chr>   
#> 1 Alonso  
#> 2 Álvarez 
#> 3 Zapatero

Upvotes: 2

Related Questions