Created
January 27, 2022 06:11
-
-
Save tatut/444feea10a1ef8484662293983e4b5f0 to your computer and use it in GitHub Desktop.
A naive natural string sort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn sort-natural [names] | |
(sort-by | |
(fn [name] | |
(mapv (fn [s] | |
(if (every? #(Character/isDigit %) s) | |
(Long/parseLong s) | |
s)) | |
(map second (re-seq #"(\d+|[^\d]+)" name)))) | |
names)) | |
;; (sort-natural ["mun dokkari 13.doc" "mun dokkari 2.doc"]) | |
;; => ("mun dokkari 2.doc" "mun dokkari 13.doc") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment