-1
(let [mymap (into {} (for [x (shuffle (rest (clojure.string/split "abcdefghijklmnopqrstuvwxyz" #"")))]
                       {x {:idx (rand-int 24)}}))]
  (into (sorted-map-by (fn [k1 k2]
                         (compare [(get-in mymap [k1 :idx]) k1]
                                  [(get-in mymap [k2 :idx]) k2])))
        mymap))

or

(let [mymap (into {} (for [x (shuffle (rest (clojure.string/split "abcdefghijklmnopqrstuvwxyz" #"")))]
                       {x {:idx (rand-int 24)}}))]
  (->> mymap
       (sort-by (fn [[_ m]] (:idx m)))
       (into (array-map))))

=> {"d" {:idx 22}, "n" {:idx 22}, "z" {:idx 14}, "w" {:idx 11}, "s" {:idx 17}, "f" {:idx 20}, "e" {:idx 19}, "q" {:idx 12}, "p" {:idx 10}, "j" {:idx 0}, "x" {:idx 20}, "v" {:idx 14}, "a" {:idx 1}, "t" {:idx 13}, "i" {:idx 21}, "k" {:idx 16}, "b" {:idx 23}, "r" {:idx 3}, "y" {:idx 18}, "g" {:idx 0}, "l" {:idx 16}, "u" {:idx 20}, "h" {:idx 10}, "m" {:idx 16}, "o" {:idx 11}, "c" {:idx 4}}

If we limit the amount to 8 is ok.

(let [mymap (into {} (for [x (shuffle (rest (clojure.string/split "abcdefghijklmnopqrstuvwxyz" #"")))]
                       {x (rand-int 24)}))]
  (into (sorted-map-by (fn [k1 k2]
                         (compare [(get mymap k1) k1]
                                  [(get mymap k2) k2])))
        (take 8 mymap)))

{"z" {:idx 1}, "q" {:idx 6}, "n" {:idx 7}, "s" {:idx 7}, "f" {:idx 9}, "d" {:idx 17}, "w" {:idx 18}, "e" {:idx 21}}

may be associated with Clojurescript Array-Map order

2
  • 2
    What is the question? Commented Jul 25, 2015 at 1:36
  • Is one big question. Commented Jul 25, 2015 at 8:10

1 Answer 1

2

What version of clojurescript are you using? The first form in cljs 0.0-3308 keeps the sort order as you expect:

    (println *clojurescript-version*) ;=>  "0.0-3308"

    (let [mymap (into {} (for [x (shuffle (rest (clojure.string/split "abcdefghijklmnopqrstuvwxyz" #"")))]
                           {x {:idx (rand-int 24)}}))]
      (into (sorted-map-by (fn [k1 k2]
                             (compare [(get-in mymap [k1 :idx]) k1]
                                      [(get-in mymap [k2 :idx]) k2])))
            mymap))
    ;=> {"d" {:idx 0}, "i" {:idx 0}, "w" {:idx 5}, "n" {:idx 6}, "q" {:idx 9}, "y" {:idx 9}, "k" {:idx 10}, "m" {:idx 10}, "x" {:idx 10}, "c" {:idx 12}, "h" {:idx 12}, "l" {:idx 13}, "v" {:idx 14}, "s" {:idx 15}, "z" {:idx 16}, "j" {:idx 17}, "p" {:idx 17}, "t" {:idx 17}, "o" {:idx 18}, "r" {:idx 19}, "b" {:idx 20}, "e" {:idx 20}, "a" {:idx 21}, "f" {:idx 21}, "g" {:idx 22}, "u" {:idx 23}}

Are you using a version older than 2411? That's the first release that includes the fix for the issue explained in the answer to the question you linked. It was fixed on this commit which was first released in version 2411.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.