Re: Sorting a collection on multiple fields

2013-09-12 Thread ulsa
The thing is that I need to provide some kind of sorting specification in my Pedestal app, and I thought the MongoDB way of using an object with { field: 1/-1 } was a nice spec. That's why I went all the way up to a function that takes such a spec. Obviously, I can't use the (. f (compare x y))

Re: Sorting a collection on multiple fields

2013-09-11 Thread Jay Fields
No - I honestly missed that version of yours the first time. I like that solution, and prefer it to the sort-by-map final solution. Probably just a style pref though. On Wed, Sep 11, 2013 at 6:07 PM, ulsa wrote: > Interesting. You used recursion, where I tried to see it as a sequence. Any > other

Re: Sorting a collection on multiple fields

2013-09-11 Thread ulsa
Interesting. You used recursion, where I tried to see it as a sequence. Any other differences? My solution was: (defn compare-many [comps] (fn [xs ys] (if-let [result (first (drop-while zero? (map (fn [f x y] (. f (compare x y))) comps xs ys)))] result 0))) (->> [{:name "zack

Re: Sorting a collection on multiple fields

2013-09-11 Thread ulsa
No. I think you missed the part that I want to be able to sort either ascending or descending on each key: (array-map :name 1 :age -1) On Saturday, 31 August 2013 15:04:44 UTC+2, Leonardo Borges wrote: > > Would this help? > > (sort-by (juxt :key1 :key2) your-list-of-maps) > > On 31/08/2013 7

Re: Sorting a collection on multiple fields

2013-08-31 Thread Jay Fields
I would solve it like this- (defn multi-compare [[c & cs] [x & xs] [y & ys]] (if (= x y) (multi-compare cs xs ys) (c x y))) (defn multi-comparator [comparators] (fn [xs ys] (if (= (count xs) (count ys) (count comparators)) (multi-compare comparators xs ys)

Re: Sorting a collection on multiple fields

2013-08-31 Thread Leonardo Borges
Would this help? (sort-by (juxt :key1 :key2) your-list-of-maps) On 31/08/2013 7:57 PM, "ulsa" wrote: > I wanted to sort a sequence of maps using a spec consisting of an ordered > map of key and order, like this: > > (array-map :name 1 :age -1) > > I couldn't find a ready-made solution, so I rol