Welcome! Please see the About page for a little more info on how this works.

0 votes
in Clojure by

There are some cases when I need to do reversed sorting. While I could pass in a comparer function that flips the result (sort #(compare %2 %1) coll), it takes a second before realizing the collection is being reversed.

It would be nice to have a couple functions that did this in a more declarative manner (function names up for suggestions).

(defn rsort
  "Same as sort, but reversed."
  ([coll] (rsort compare coll))
  ([comp coll] (sort #(comp %2 %1) coll)))

(defn rsort-by
  "Same as sort-by, but reversed."
  ([keyfn coll] (rsort-by keyfn compare coll))
  ([keyfn comp coll] (sort-by keyfn #(comp %2 %1) coll)))

I wound up adding these to a shared library for my projects, but it would be great to have something like this part of Clojure's core library (more selfishly, so that I don't need to require/namespace these functions in my projects).

1 Answer

0 votes
selected by
Best answer

I think it’s more likely we would add a reverse comparator than special r whatever functions.

There may already be a ticket for that though.