This is a helpful hint, and probably one that others are already
using, but seemed good to share.

If you're sorting a list of maps on one field, you can use
    (sort-by :community list-of-residents)

But sorting on two (or more) fields seems to require a little more
coding:
    (sort-by :community (sort-by :last-name list-of-residents))

or
    (sort-by #(vector (:community %) (:last-name %)) list-of-
residents)

But the latter version is equivalent to the terser
    (sort-by (juxt :community :last-name) list-of-residents)

The juxt function takes functions as an argument (and this includes
function equivalents like keywords) and returns a function that
generates a vector by applying each function in order.  (Not clear
enough?  Well, from the docstring for juxt:

    Takes a set of functions and returns a fn that is the
juxtaposition of those
    fns.  The returned fn takes a variable number of args, and returns
a vector
    containing the result of applying each fn to the args (left-to-
right).

Anyway, the resulting code for sort-by is terse and clear (even if the
reader is not sure about juxt, the prominent key-fields may give the
correct idea).

Thanks for your patience,
ron

-- 
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

Reply via email to