【发布时间】:2014-12-03 17:05:55
【问题描述】:
所以,这是我的问题 -
我想根据一组确定排序字段和顺序的变量来排序列表
本质上我需要动态地“排序”。
EG:
declare function getSearchResults(
$query as cts:query,
$sort as xs:string*,
$direction as xs:string*,
) as element()* {
let $results :=
cts:search(/*, $query)
let $sortFields := fn:tokenize($sort, "\|")
let $dec := $direction = 'desc' or $direction = 'descending'
let $sorted := sortByFields($results, $sort,$dec)
return $sorted
};
declare private function sortByFields ($results, $sortFields, $dec)
{
let $asc := fn:not($dec)
for $i in $results
order by
if ($sortFields[1]='id' and $asc) then $i//ldse:document/@id else (),
if ($sortFields[1]='id' and $dec) then $i//ldse:document/@id else () descending,
if ($sortFields[1]='title' and $asc) then $i//title else (),
if ($sortFields[1]='title' and $dec) then $i//title else () descending
return if (fn:count($sortFields) > 1 ) then (sortByFields($i,$sortFields[2 to fn:count($sortFields)],$dec)) else ($i)
};
此方法不起作用,因为它必须多次排序,并且不会保留每次迭代的排序顺序。
我也试过这个:
let $sortFields := fn:tokenize($sort, "\|")
let $dec := $direction = 'desc' or $direction = 'descending'
let $asc := fn:not($dec)
for $i in $results
for $j in 1 to fn:count($sortFields)
order by
if ($sortFields[$j]='id' and $asc) then $i//ldse:document/@id else (),
if ($sortFields[$j]='id' and $dec) then $i//ldse:document/@id else () descending,
if ($sortFields[$j]='title' and $asc) then $i//title else (),
if ($sortFields[$j]='title' and $dec) then $i//title else () descending
return $i
但这会重复我的数据。 (返回按每个排序字段排序)
由于代码注入,我不想使用 xdmp:eval,有什么办法可以做到这一点吗?
任何帮助或建议将不胜感激。
非常感谢!
【问题讨论】: