Skip to content

Instantly share code, notes, and snippets.

@line-o
Created May 4, 2021 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save line-o/d4314b3464f4f2959853780c43672d12 to your computer and use it in GitHub Desktop.
Save line-o/d4314b3464f4f2959853780c43672d12 to your computer and use it in GitHub Desktop.
distinct values from a given list in the order appearance
xquery version "3.1";
declare function local:distinct-order ($to-sort, $order) {
for $item in $to-sort[.=$order]
let $group-key := $item
group by $group-key
order by index-of($order, $group-key)[1]
return $group-key
};
[
local:distinct-order((2, 4, 5, 4, 6, 1, 2, 3), (1, 2, 3, 4, 5, 6)),
local:distinct-order((2, 4, 51, 4, 6, 111, 2, 3), (111, 2, 3, 4, 51, 6))
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment