Skip to content

Instantly share code, notes, and snippets.

@tatut
Created January 27, 2022 06:11
Show Gist options
  • Save tatut/444feea10a1ef8484662293983e4b5f0 to your computer and use it in GitHub Desktop.
Save tatut/444feea10a1ef8484662293983e4b5f0 to your computer and use it in GitHub Desktop.
A naive natural string sort
(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