I don't know if it is a more elegant implementation, but I found something like this for maps in the useful library a while back, called ordering-map:
https://github.com/amalloy/useful/blob/master/src/flatland/useful/map.clj#L243-L245 I have been putting a few different varieties of sorted maps and sets, including ordering-maps, into the Sets/Create and Maps/Create categories of the Clojure cheat sheet here: http://jafingerhut.github.io/cheatsheet/clojuredocs/cheatsheet-tiptip-cdocs-summary.html Andy On Thu, Mar 19, 2015 at 5:53 AM, Henrik Heine <h3nr1k.h3...@googlemail.com> wrote: > Hi, > > I want to sort a set/map according to an ordering given by a seq of > elements - e.g. > > (def some-order [:u :a :e :i :o]) > (def some-order-fn (order-fn some-order)) > (sorted-set-by some-order-fn :a :e :i :o :u) ; --> #{:u :a :e :i :o} > > This is what I came up with: > > (defn order-fn [ks] > #(- (.indexOf ks %1) > (.indexOf ks %2))) > > But then one may want to replace .indexOf for some other ord-function- > like this: > > (defn order-fn > ([ks] (order-fn ks #(.indexOf %1 %2))) > ([ks ord-fn] > #(- (ord-fn ks %1) > (ord-fn ks %2)))) > > Now we may force the elements to be present in ks: > > (defn order-fn > ([ks] (order-fn ks #(.indexOf %1 %2))) > ([ks ord-fn] > (letfn > [(-ord-fn [e] > (let [o (ord-fn ks e)] > (if (> o -1) o > (throw > (RuntimeException. > (format "'%s' not found in %s" e ks))))))] > #(compare (-ord-fn %1) > (-ord-fn %2))))) > > Is there something like this in clojure.core already or a more elegant > implementation? > > -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to clojure@googlegroups.com > Note that posts from new members are moderated - please be patient with > your first post. > To unsubscribe from this group, send email to > clojure+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/clojure?hl=en > --- > You received this message because you are subscribed to the Google Groups > "Clojure" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to clojure+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.