title: Marklogic - Sorting a Map
body: <p>I am trying to count the most commonly used terms from a specific field of an executed search. I am then trying to sort the Map, but haven't had any success with order by or found any functions that would work.</p>
  
  <p>Can anyone advise or point me to the docs that detail how to properly sort a map?</p>
  
  <p>Here is a sample  map</p>
  
  <pre><code>&lt;map:map xmlns:map="http://marklogic.com/xdmp/map" 
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;map:entry key="Orlando"&gt;
      &lt;map:value xsi:type="xs:integer"&gt;2&lt;/map:value&gt;
    &lt;/map:entry&gt;
    &lt;map:entry key="Christiantiy"&gt;
      &lt;map:value xsi:type="xs:integer"&gt;3&lt;/map:value&gt;
    &lt;/map:entry&gt;
    &lt;map:entry key="Fort Lauderdale"&gt;
      &lt;map:value xsi:type="xs:integer"&gt;2&lt;/map:value&gt;
    &lt;/map:entry&gt;
    &lt;map:entry key="Key West"&gt;
      &lt;map:value xsi:type="xs:integer"&gt;3&lt;/map:value&gt;
    &lt;/map:entry&gt;
    &lt;map:entry key="David Robinson"&gt;
      &lt;map:value xsi:type="xs:integer"&gt;2&lt;/map:value&gt;
    &lt;/map:entry&gt;
    &lt;map:entry key="Florida"&gt;
      &lt;map:value xsi:type="xs:integer"&gt;8&lt;/map:value&gt;
    &lt;/map:entry&gt;
    &lt;map:entry key="test"&gt;
      &lt;map:value xsi:type="xs:integer"&gt;2&lt;/map:value&gt;
    &lt;/map:entry&gt;
  &lt;/map:map&gt;
  </code></pre>
  
  <p>Right now I am trying to a second loop through the map, (removing everything with count of 1), and trying to add an order by to it, with no luck.</p>
  
  <pre><code>let $_ :=
    for $tag in map:keys($temp)
    order by map:get($temp, $tag)
     return
  
        if (map:get($temp, $tag) = 1) then
          ()
        else
          let $_ := map:put($temp2, $tag, map:get($temp,$tag))
          return $_
  
  return $temp2
  </code></pre>