Sort multiple cards in marklogic 8

This is XQuery rather than MarkLogic. I have a card with three cards: a card and each card have a key-value pair and a score. I would like to sort all the individual identifiers based on the score from each card. For instance,

map1 : 1:2048, 5:2000
map2 : 2:5000, 1:1000, 4:3000
map3 : 6:100, 7:5000, 2:2000

In the above example, each card has an identifier: an estimate for the key value (I don’t know how to present it here :)) ..

I want a sorted id list of three maps based on the score.

Is there a good way or a better way to sort, or do I need to combine the card keys and iterate over the sequence of keys and sort them?

+4
source share
1 answer

. Xquery 3.0.

. $combinedMaps , $mapToMerge - , .

, .

   declare function local:sortMaps(
  $newMap as map:map,
  $mapA as map:map,
  $mapB as map:map
) as map:map {
  let $build :=
    for $key in map:keys($mapA)
    let $otherMapValue :=
      (map:get($mapB, $key), 0)[1]
    let $value := map:get($mapA, $key)
    return 
      if ($value gt $otherMapValue) then (
        map:put($newMap, $key, $value)
      ) else (
        map:put($newMap, $key, $otherMapValue)
      )
  return $newMap
};

let $map1 := 
  map:new((
    map:entry("1",2048),
    map:entry("5",2000)
  ))

let $map2 := 
  map:new((
    map:entry("2",5000),
    map:entry("1",1000),
    map:entry("4",3000)
  ))

let $map3 := 
  map:new((
    map:entry("6",100),
    map:entry("7",5000),
    map:entry("2",2000)
  ))

let $maps := ($map1, $map2, $map3)
return
  fn:fold-left(
    function($combinedMaps, $mapToMerge) {
      let $newMap := map:map()
      let $newMap := local:sortMaps($newMap, $combinedMaps, $mapToMerge)
      let $newMap := local:sortMaps($newMap, $mapToMerge, $combinedMaps)
      return $newMap
    }, 
    $maps[1], 
    $maps
  )
+3

All Articles