Search code examples
arraysvectorclojure

How to insert into clojure vector based on ordering key


I have the following clojure vector where each element is an ArrayMap:

[{:title "Step 2", :order 1}
 {:title "Step 1", :order 0}
 {:title "Step 3", :order 2}]

and I want to organize it into a new vector which contains each title indexed based on its corresponding order number as follows:

["Step 1" "Step 2" "Step 3"]

How can I do this?


Solution

  • > (map :title (sort-by :order v))
    ("Step 1" "Step 2" "Step 3")
    

    If you really need the result to be a vector:

    > (mapv :title (sort-by :order v))
    ["Step 1" "Step 2" "Step 3"]