1

I don't understand why cljs.reader/read-string function reads my edn-file's content in a random order. Here is an example file content:

{:data
 {:name-tit
  {:hu "Név"
   :en "Name"}

  :description-tit
  {:hu "Leírás"
   :en "Description"}

  :short-description-tit
  {:hu "Rövid leírás"
   :en "Short description"}

  :title-tit
  {:hu "Cím"
   :en "Title"}

  :label-tit
  {:hu "Címke"
   :en "Label"}

  :color-tit
  {:hu "Szín"
   :en "Color"}

  :color-gradient-tit
  {:hu "Színátmenet"
   :en "Color gradient"}

  :link-tit
  {:hu "Link"
   :en "Link"}

  :target-tit
  {:hu "Link target"
   :en "Link target"}

  :weight-tit
  {:hu "Tömeg"
   :en "Weight"}

  :length-tit
  {:en "Length"
   :hu "Hossz"}

  :width-tit
  {:en "Width"
   :hu "Szélesség"}

  :height-tit
  {:en "Height"
   :hu "Magasság"}}}

And this is the output of the read-string function:

{:data {:weight-tit {:hu "Tömeg", :en "Weight"}, :color-tit {:hu "Szín", :en "Color"}, :height-tit {:en "Height", :hu "Magasság"}, :short-description-tit {:hu "Rövid leírás", :en "Short description"}, :label-tit {:hu "Címke", :en "Label"}, :link-tit {:hu "Link", :en "Link"}, :title-tit {:hu "Cím", :en "Title"}, :name-tit {:hu "Név", :en "Name"}, :target-tit {:hu "Link target", :en "Link target"}, :width-tit {:en "Width", :hu "Szélesség"}, :length-tit {:en "Length", :hu "Hossz"}, :color-gradient-tit {:hu "Színátmenet", :en "Color gradient"}, :description-tit {:hu "Leírás", :en "Description"}}}

Why is this happening to me? :)

1 Answers1

2

Clojure maps are an unordered data structure. It is being read in the order that it is stored in the file, and key/value pairs are most likely being added to the map in memory while it is being read, but when you then traverse the map in memory, its key/value pairs are returned in an order related to internal implementation details of the map implementation, typically involving hash values of the keys.

If you want to preserve order, Clojure lists and vectors are guaranteed to do this. Normal maps and sets are not. There are sorted variants of the maps and sets, but using those in an EDN format file requires some special handling, that I have not used before.

andy_fingerhut
  • 1,486
  • 8
  • 10