Here is an extract from the Reference Manual. Hope this helps.
firstsortedvalue ( [ distinct ] expression [, sort-weight [, n ]] )
returns the first value of expression sorted by corresponding sortweight
when expression is iterated over a number of records as
defined by a group by clause. Sort-weight should return a numeric
value where the lowest value will render the corresponding value of
expression to be sorted first. By preceding the sort-value expression
with a minus sign, the function will return the last value instead. If
more than one value of expression share the same lowest sort-order,
the function will return null. By stating an n larger than 1, you will
get the nth value in order. If the word distinct occurs before the
expression, all duplicates will be disregarded.