# Support for XForm Sort Expression

**URL:** <https://forum.getodk.org/t/support-for-xform-sort-expression/41513>\
**Category:** Support\
**Created:** [May 12, 2023, 11:21am UTC](https://forum.getodk.org/t/support-for-xform-sort-expression/41513 "2023-05-12T11:21:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phil\_Burapha](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/phil_burapha/32/12926_2.png) [@Phil\_Burapha](https://forum.getodk.org/u/Phil_Burapha)\
**Post date:** [May 12, 2023, 11:21am UTC](https://forum.getodk.org/t/support-for-xform-sort-expression/41513/1 "2023-05-12T11:21:56Z")

</div>

I have a use case that requires the calculation of the Median value of a set collected in a Repeat Group. The use case if for measuring standing timber volumes. When you are measuring a stand you first measure the diameter of all stems in the plot. You then measure the height of a sub-set of those stems. In my use-case you measure the height of the maximum and median stem for every species present in the plot. I am able to calculate the maximum using the max() function, but there is no equivalent for median. I therefore tried adjusting the Xform code to include:

position(sort(${my\_nodeset}), count(${my\_nodeset}) div 2 + 1)

As a method of calculating the median. The form uploaded to ODK Central without issue but when I tried to use it with ODK Collect I got the following error message:

Error Occurred Error evaluating field ... The problem was located in Calculate expression for ... XPath evaluation: cannot handle function 'sort'

It seems that the XForm sort() function is not part of the ODK XForm specification.

I would like to add the XForm sort() function to enable the sorting of nodesets. This will allow me to calculate the median tree diameter in a plot.

---

<div class="post-metadata">

**Author:** ![danbjoseph](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/danbjoseph/32/21901_2.png) [@danbjoseph](https://forum.getodk.org/u/danbjoseph)\
**Post date:** [May 12, 2023, 6:48pm UTC](https://forum.getodk.org/t/support-for-xform-sort-expression/41513/2 "2023-05-12T18:48:11Z")

</div>

This is not an area I'm familiar with but have you looked at the code repository for the ODK XForms spec? The readme includes a small section on proposing specification changes.

> **[GitHub - getodk/xforms-spec: The XForms-derived specification used in the ODK...](https://github.com/getodk/xforms-spec)**
>
> The XForms-derived specification used in the ODK ecosystem. If you are interested in building a tool that is compliant with the forms rendered by ODK tools, this is the place to start. ✨⚒✨ - GitHub...

---

<div class="post-metadata">

**Author:** ![LN](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/ln/32/1605_2.png) [@LN](https://forum.getodk.org/u/LN)\
**Post date:** [May 12, 2023, 10:07pm UTC](https://forum.getodk.org/t/support-for-xform-sort-expression/41513/3 "2023-05-12T22:07:17Z")

</div>

Thanks for describing your use case in detail, @Phil_Burapha!

Given that your underlying need is for computing a median, would a `median` function work for you? I don't see any such thing in core XPath/XQuery/XForm but I do see that MarkLogic has a custom `median` function: [https://docs.marklogic.com/math:median](https://docs.marklogic.com/math:median)

I can't find a reference to an XForm `sort` function, do you have a link? I see [this article](https://en.wikibooks.org/wiki/XForms/Sorting_using_XSLT) about an XLST-based alternative (wouldn't be supported by ODK tools). It looks like Orbeon Forms has a [custom function](https://doc.orbeon.com/xforms/xpath/extension-functions/extension-xml#xxf-sort).

Thanks for pointing to that spec proposal section, @danbjoseph! We don't do formal voting these days and should update that. This thread is a fine feature proposal and will be helpful for gauging level of interest/need.

---

<div class="post-metadata">

**Author:** ![Xiphware](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/xiphware/32/2587_2.png) [@Xiphware](https://forum.getodk.org/u/Xiphware)\
**Post date:** [May 12, 2023, 11:55pm UTC](https://forum.getodk.org/t/support-for-xform-sort-expression/41513/4 "2023-05-12T23:55:43Z")

</div>

> [@LN](#):
>
> I can't find a reference to an XForm `sort` function,

I poked around too, and only found this: [https://www.w3.org/TR/xpath-functions-31/#func-sort](https://www.w3.org/TR/xpath-functions-31/#func-sort)

Which is XPath 3.1 (!), and doesnt appear to be strictly a (new) XPath function per se, but rather a so-called 'higher order function'; something that appears to involve invoking the host language... 🤔

But I agree having something at least rudimentary to permit sorting multiple values from a nodeset (or selection?) in some form could potentially be useful for a lot of applications, beyond the basic current [min()](https://docs.getodk.org/form-operators-functions/#min) & [max()](https://docs.getodk.org/form-operators-functions/#max)

---

<div class="post-metadata">

**Author:** ![Phil\_Burapha](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/phil_burapha/32/12926_2.png) [@Phil\_Burapha](https://forum.getodk.org/u/Phil_Burapha)\
**Post date:** [May 15, 2023, 6:03am UTC](https://forum.getodk.org/t/support-for-xform-sort-expression/41513/5 "2023-05-15T06:03:55Z")

</div>

Yes - a median function would be perfect. But, I was also planning to make Upper Quartile and Lower Quartile calculations using a variation of my formula. Would it be possible to add also a Quartile function?

---

<div class="post-metadata">

**Author:** ![Phil\_Burapha](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/phil_burapha/32/12926_2.png) [@Phil\_Burapha](https://forum.getodk.org/u/Phil_Burapha)\
**Post date:** [July 19, 2023, 9:05am UTC](https://forum.getodk.org/t/support-for-xform-sort-expression/41513/6 "2023-07-19T09:05:56Z")

</div>

@Xiphware and @LN is there a way that I can boost this? To simplify the requirement is for a median() and quartile() function.

---

<div class="post-metadata">

**Author:** ![seewhy](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/seewhy/32/3199_2.png) [@seewhy](https://forum.getodk.org/u/seewhy)\
**Post date:** [September 1, 2025, 8:16am UTC](https://forum.getodk.org/t/support-for-xform-sort-expression/41513/7 "2025-09-01T08:16:01Z")

</div>

I have a use case for median() - or a calculation that would get me there. Has there been any progress with this?

Thanks.
