如何根据另一个向量的值对一个向量进行排序?
说我有预定义的顺序:
(def order ["0M","6M","1Y","2Y","3Y"])
我有另一个向量["0M","1Y","6M"]
(可能包含也可能不包含向量“order”的所有元素)
输出应为["0M","6M","1Y"]
答案 0 :(得分:5)
(def order ["0M","6M","1Y","2Y","3Y"])
(sort-by #(.indexOf order %) ["0M", "1Y", "6M"]) ; ("0M" "6M" "1Y")
请注意sort-by
返回一个序列。如果您绝对需要矢量结果,则可以将输出提供给vec
。