Skip to content

Instantly share code, notes, and snippets.

@dfuenzalida
Created June 29, 2021 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dfuenzalida/6d5b99d94949e4642140d43f86ae9dfa to your computer and use it in GitHub Desktop.
Save dfuenzalida/6d5b99d94949e4642140d43f86ae9dfa to your computer and use it in GitHub Desktop.
Sort strings numerically in Clojure
(defn digits-in-str [s]
(->> (re-seq #"\d+" s) first))
(def items ["item_20" "item_11" "item_1" "item_120"])
(comment
(sort-by (comp clojure.edn/read-string digits-in-str) items)
;; => ("item_1" "item_11" "item_20" "item_120")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment