Groups a result set by one or more fields. This function works in an almost identical way to the standard group function, with just one difference: The key-value pairs in the map returned are ordered the same way as the data in the original result set. e.g. The standard group function returns maps with keys that appear to be sorted according to their natural order.
Because of the similarity, this explanation uses the same result examples as for the group function.
We'll use the same set of examples as for the standard group function.
Hence lets consider grouping the result set by the id
field.
:-) groupPreserveOrder( results, "id" );
There is 1 result
{
a=[{name=Elias, design=1, id=a}],
b=[{name=Alexander, design=1, id=b}],
c=[{name=Isabelle, design=1, id=c}],
d=[{name=Eirene, design=1, id=d}],
e=[{name=Isabelle, design=1, id=e}],
f=[{name=Kisha, design=2, id=f}],
g=[{name=Martina, design=2, id=g}],
h=[{name=Xavier, design=2, id=h}],
i=[{name=Xavier, design=2, id=i}],
j=[{name=Mikolás, design=3, id=j}],
k=[{name=Scrapper, design=3, id=k}],
l=[{name=Elias, design=3, id=l}],
m=[{name=Martina, design=4, id=m}],
n=[{name=Mervin, design=4, id=n}],
o=[{name=Kisha, design=5, id=o}],
p=[{name=Martina, design=6, id=p}]
}
The value returned is identical to the standard group function, as is
the result when grouping by the design field. This is a
coincidence of the field values chosen.
Now lets consider what happens when we group by the name
field.
:-) groupPreserveOrder( results, "name" );
There is 1 result
{
Elias=[
{name=Elias, design=1, id=a},
{name=Elias, design=3, id=l}
],
Alexander=[{name=Alexander, design=1, id=b}],
Isabelle=[
{name=Isabelle, design=1, id=c},
{name=Isabelle, design=1, id=e}
],
Eirene=[{name=Eirene, design=1, id=d}],
Kisha=[
{name=Kisha, design=2, id=f},
{name=Kisha, design=5, id=o}
],
Martina=[
{name=Martina, design=2, id=g},
{name=Martina, design=4, id=m},
{name=Martina, design=6, id=p}
],
Xavier=[
{name=Xavier, design=2, id=h},
{name=Xavier, design=2, id=i}
],
Mikolás=[{name=Mikolás, design=3, id=j}],
Scrapper=[{name=Scrapper, design=3, id=k}],
Mervin=[{name=Mervin, design=4, id=n}]
}
The value returned this time is different from that returned by the standard group function. This time the keys in the returned map are defined in the same order they first appeared in the result set. Hence the first key is "Elias", the second "Alexander", etc.
Aside from the ordering, as explained in the Single level grouping section, Multiple level grouping works the same way as the standard group function.