Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting latin strings in Clojure

I did this experiment in the REPL:

(sort ["maa" "ácw" "ijl" "aez" "jkl"])

I got this:

("aez" "ijl" "jkl" "maa" "ácw")

The right answer is this:

("ácw" "aez" "ijl" "jkl" "maa")

Is there a way to sort Latin strings in Clojure natively? If not why not?

Look I didn't ask how to sort this, I know I can pass the seq through a pipe and substitute the non ANSI chars.

like image 504
Carlos Nunes Avatar asked Nov 30 '25 02:11

Carlos Nunes


1 Answers

You can use the Collator class, on the REPL:

=> (import java.util.Locale)
java.util.Locale
=> (import java.text.Collator)
java.text.Collator
=> (def collator (Collator/getInstance (Locale. "pt_BR")))
#'user/collator
=> (sort collator ["maa" "ácw" "ijl" "aez" "jkl"])
("ácw" "aez" "ijl" "jkl" "maa")

In this example I'm using the Brazilian Locale. You need to change this locale to the one you want to use. There is a list of supported locales here.

like image 68
Rodrigo Taboada Avatar answered Dec 05 '25 08:12

Rodrigo Taboada



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!