Skip to content

Instantly share code, notes, and snippets.

@dfuenzalida
Created June 29, 2021 06:39
Show Gist options
  • 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