map behavior ?
Tuesday, 26 September 2017 · 3 messages
JM
jean-marc Mercier26 Sep 2017 11:29 #
Hello BaseX team,
It has been a while since I last wrote to this mailing list ! Happy to report again :)
I have a strange behavior concerning map, it doesn't seem to sort as I expected. Is there any explanation ?
let $map as map(xs:double,xs:string) := map:merge((
map:entry(xs:double(0.1), "Sunday"),
map:entry(xs:double(5.0), "Thursday"),
map:entry(xs:double(2.0), "Monday"),
map:entry(xs:double(3.0), "Wednesday"),
map:entry(xs:double(0.5), "Tuesday"),
map:entry(xs:double(6.0), "Friday"),
map:entry(xs:double(7.0), "Saturday")
))
return $mapoutput :
map {
2: "Monday",
3: "Wednesday",
5: "Thursday",
6: "Friday",
7: "Saturday",
0.1: "Sunday",
0.5: "Tuesday"
}
Cheers
CG
Christian Grün26 Sep 2017 12:01 #
Dear Jean-Marc,
Welcome back!
Maps in XQuery are simple key/value pairs; neither keys nor values will be arranged in a specific order.
If you want to sort map by keys before outputting them, you can sort them in a second step:
let $map := map:merge((
map:entry(0.1, "Sunday"),
map:entry(5.0, "Thursday"),
map:entry(2.0, "Monday"),
map:entry(3.0, "Wednesday"),
map:entry(0.5, "Tuesday"),
map:entry(6.0, "Friday"),
map:entry(7.0, "Saturday")
))
for $key in sort(map:keys($map))
return "* " || $key || ": " || $map($key)Hope this helps,
Christian
Quoted text · jean-marc Mercier · 33 lines
On Tue, Sep 26, 2017 at 11:29 AM, jean-marc Mercier <jeanmarc.mercier at gmail.com> wrote: > Hello BaseX team, > > It has been a while since I last wrote to this mailing list ! Happy to > report again :) > > I have a strange behavior concerning map, it doesn't seem to sort as I > expected. Is there any explanation ? > > let $map as map(xs:double,xs:string) := map:merge(( > map:entry(xs:double(0.1), "Sunday"), > map:entry(xs:double(5.0), "Thursday"), > map:entry(xs:double(2.0), "Monday"), > map:entry(xs:double(3.0), "Wednesday"), > map:entry(xs:double(0.5), "Tuesday"), > map:entry(xs:double(6.0), "Friday"), > map:entry(xs:double(7.0), "Saturday") > )) > return $map > > output : > map { > 2: "Monday", > 3: "Wednesday", > 5: "Thursday", > 6: "Friday", > 7: "Saturday", > 0.1: "Sunday", > 0.5: "Tuesday" > } > > Cheers
JM
jean-marc Mercier26 Sep 2017 15:52 #
Hello Christian !
Nice to read you again ! I am striking back to BaseX, using it as my main database server for a integrating / pricing / unittesting engine. Ok, fair enough, I implement a sort_map function.
2017-09-26 12:01 GMT+02:00 Christian Grün <christian.gruen at gmail.com>:
Quoted text · Christian Grün · 62 lines
> Dear Jean-Marc, > > Welcome back! > > Maps in XQuery are simple key/value pairs; neither keys nor values > will be arranged in a specific order. > > If you want to sort map by keys before outputting them, you can sort > them in a second step: > > let $map := map:merge(( > map:entry(0.1, "Sunday"), > map:entry(5.0, "Thursday"), > map:entry(2.0, "Monday"), > map:entry(3.0, "Wednesday"), > map:entry(0.5, "Tuesday"), > map:entry(6.0, "Friday"), > map:entry(7.0, "Saturday") > )) > for $key in sort(map:keys($map)) > return "* " || $key || ": " || $map($key) > > Hope this helps, > Christian > > > > > On Tue, Sep 26, 2017 at 11:29 AM, jean-marc Mercier > <jeanmarc.mercier at gmail.com> wrote: > > Hello BaseX team, > > > > It has been a while since I last wrote to this mailing list ! Happy to > > report again :) > > > > I have a strange behavior concerning map, it doesn't seem to sort as I > > expected. Is there any explanation ? > > > > let $map as map(xs:double,xs:string) := map:merge(( > > map:entry(xs:double(0.1), "Sunday"), > > map:entry(xs:double(5.0), "Thursday"), > > map:entry(xs:double(2.0), "Monday"), > > map:entry(xs:double(3.0), "Wednesday"), > > map:entry(xs:double(0.5), "Tuesday"), > > map:entry(xs:double(6.0), "Friday"), > > map:entry(xs:double(7.0), "Saturday") > > )) > > return $map > > > > output : > > map { > > 2: "Monday", > > 3: "Wednesday", > > 5: "Thursday", > > 6: "Friday", > > 7: "Saturday", > > 0.1: "Sunday", > > 0.5: "Tuesday" > > } > > > > Cheers >
Written for BaseX 8.6. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery