clojurescriptre-frame

Clojure sort map object key


I have a flowers object;

Like this.

enter image description here

   :flowers
           {:flower_1  "orchid"
            :flower_2  "Gazania"
            :flower_3  "Cockscomb"
            :flower_4  "Yarrow"  
            :flower_5  "Trollius" 
            :flower_6  "Soapwort" 
            :flower_7  "Gardenia"
            :flower_8  "Antirrhinum" 
            :flower_9  "Ginger" 
            :flower_10 "Trollius"
            :flower_11 "Gazania"
            :flower_12 "Knapweed"
             }

enter image description here

When ı upload object to state. Mixed sort, Like this.

So ı decide sort flowers by key when get flowers from state.

Why ı need, ı sort flowers keys, Because get glowers after listed in views.

enter image description here

I need sort

[:div 
  [:h1 "Set Flowers"]
        (for [[key value] @flowers]
            (edit-flower-input key value))]

(defn edit-flower-input [id label]
  (let [value (re-frame/subscribe [::subs/get-flower id])]
  [:div {:key id :class "row"}
  [:label id ]
    [:input.input {:value @value
                    :class "form-control"
                    :on-change #(re-frame/dispatch [::events/update-flower id (-> % .-target .-value)])
                    :type "text"
                    :placeholder label}]
    ] ))


Solution

  • [:div 
      [:h1 "Set Flowers"]
            (for [[key value] (sort-by key @flowers)]
                (edit-flower-input key value))]