Custom sort order
The sort function sorts a collection based on its "natural ordering." sort-by allows you to sort a collection based on the natural ordering of the result of some function applied to the elements. Your task is to write a function sort-with which takes a sequence of elements that define the sort order.
Example
; define ordering ; collection to sort
(sort-with [:breakfast :lunch :dinner] #{:lunch :breakfast :dinner}) ;=> (:breakfast :lunch :dinner)
(sort-with [2 3 4 :jack :queen :king :ace] [4 2 4 :king 2]) ;=> [2 2 4 4 :king]Thanks to this site for the problem idea, where it is rated Expert in PHP. The problem has been modified.
Please submit your solutions as comments on this gist.
To subscribe: https://ericnormand.me/newsletter
Here is my solution with mapped indices (Clojure beginner):
Elements that are not in
order-seqwill appear at the beginning of the sorted result, since their index will be nil.@jonasseglare s solution seems much more efficient and elegant though, would have never occurred to me!