2

如何根据另一个向量的值对一个向量进行排序?

假设我有预定义的顺序:

(def order ["0M","6M","1Y","2Y","3Y"])

我有另一个向量["0M","1Y","6M"](可能包含也可能不包含向量“订单”的所有元素)

输出应该是["0M","6M","1Y"]

4

1 回答 1

5
(def order ["0M","6M","1Y","2Y","3Y"])

(sort-by #(.indexOf order %) ["0M", "1Y", "6M"]) ; ("0M" "6M" "1Y")

请注意,它sort-by返回一个序列。如果您绝对需要矢量结果,可以将输出提供给vec.

于 2012-09-10T01:55:42.143 回答