Reputation: 23
How do I sort one vector based on the values of another?
Say I have predefined order:
(def order ["0M","6M","1Y","2Y","3Y"])
I have another vector ["0M","1Y","6M"]
(may or may not contain all elements of vector "order")
Output should be ["0M","6M","1Y"]
Upvotes: 2
Views: 833
Reputation: 3378
(def order ["0M","6M","1Y","2Y","3Y"])
(sort-by #(.indexOf order %) ["0M", "1Y", "6M"]) ; ("0M" "6M" "1Y")
Notice that sort-by
returns a sequence. If you absolutely need a vector result, you can feed the output to vec
.
Upvotes: 5