I want to order a sequence of maps with keys:
obligatory :type
optional :before [types]; which means the types should occur before
this element in the sequence.
I tried to use a custom java.util.Comparator but it only compares
adjacent elements.
This is my example:
(defn- comes-after?
"checks if o1 comes after o2 if o1 has an :after seq of types;"
[o1 o2]
(some #{(:type o2)} (:after o1)))
; Compares its two arguments for order. Returns a negative integer,
zero, or a positive integer as
; the first argument is less than, equal to, or greater than the
second.
(def mycomparator
(reify java.util.Comparator
(compare [this o1 o2]
(println "comparing o1: " o1 ", and o2: " o2)
(cond
(comes-after? o1 o2) 1
(comes-after? o2 o1) -1
:else 0))))
; Dieser algorithmus funzt net da immer nur die adjazenten elemente
verglichen werden, :a und :o werden nur verglichen wenn nebeneinander
(def data [{:type :a :after [:o]}
{:type :b}
{:type :z}
{:type :e}
{:type :m}
{:type :o}])
(sort mycomparator data)
Which leads to the following printlns:
comparing o1: {:type :a, :after [:o]} , and o2: {:type :b}
comparing o1: {:type :b} , and o2: {:type :z}
comparing o1: {:type :z} , and o2: {:type :e}
comparing o1: {:type :e} , and o2: {:type :m}
comparing o1: {:type :m} , and o2: {:type :o}
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en