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
The complexity of this code is linear with the size of the collection to sort:
Elements in
coll
that don't exist inorder
are excluded from the sorted result.