在 Clojure 中对拉丁字符串进行排序
Sorting latin strings in Clojure
我在 REPL 中做了这个实验:
(sort ["maa" "ácw" "ijl" "aez" "jkl"])
我知道了:
("aez" "ijl" "jkl" "maa" "ácw")
正确答案是:
("ácw" "aez" "ijl" "jkl" "maa")
有没有办法在 Clojure 中对拉丁字符串进行本地排序?如果不是,为什么不呢?
你看我没有问如何排序,我知道我可以通过管道传递 seq 并替换非 ANSI 字符。
您可以在 REPL 上使用 Collator
class:
=> (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")
在此示例中,我使用的是巴西语 Locale
. You need to change this locale to the one you want to use. There is a list of supported locales here。
我在 REPL 中做了这个实验:
(sort ["maa" "ácw" "ijl" "aez" "jkl"])
我知道了:
("aez" "ijl" "jkl" "maa" "ácw")
正确答案是:
("ácw" "aez" "ijl" "jkl" "maa")
有没有办法在 Clojure 中对拉丁字符串进行本地排序?如果不是,为什么不呢?
你看我没有问如何排序,我知道我可以通过管道传递 seq 并替换非 ANSI 字符。
您可以在 REPL 上使用 Collator
class:
=> (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")
在此示例中,我使用的是巴西语 Locale
. You need to change this locale to the one you want to use. There is a list of supported locales here。