Symptom
Numeric sorting on a repeater fails with Numeric sort with XPath 'field_name' encountered NaN value ''.
Text sorting on the same repeater works.
Wrapping the value in number(.) does not help when some records are empty or non-numeric, which is exactly when the error appears.
Cause
Numeric sorting converts every selected sort value to a number.
An empty string or non-numeric text converts to NaN, and a single such row fails the whole sort.
Switching the sort type to text makes the error disappear, but that is not a fix: text ordering puts "10" before "9", so the output is now silently wrong instead of loudly broken. Loudly broken is the better of the two.
Fix
Guard the sort value and supply a deterministic fallback for invalid rows. The XPath 1.0 pattern reported for this:
value[number()=number()] | default
It works because NaN is the one value not equal to itself, so the number()=number() predicate filters out rows that fail numeric conversion, and the union with a default variable (typically 0) gives those rows a sortable value.
Two design decisions to make consciously:
- Should missing values sort first or last? Choose the fallback accordingly, before someone in production asks why the empty rows are on top.
- Is the fallback acceptable at all, or should the source data be repaired, or a dedicated always-present numeric sort field mapped instead? The upstream fix is more work and more durable.
Test with empty, non-numeric, negative, decimal, and duplicate values. This mechanism came from community experience, so validate it against the XPath implementation of your installed StoryTeller version before deploying it broadly.
Do not hide numeric errors with text sorting
Never use a text sort merely to suppress this error when the business rule requires numeric ordering. The error is telling you the data has rows that cannot be ordered numerically; the correct response is to decide what those rows should do, not to change the question.