Sort entries within repeating fields

How can I sort all entries in a repeating field in ascending (or descending order), and how can I sort more than one repeating field at the same time?

The setup consists of:

TO::array, the Repeating Field to sort, where TO is the name of the table occurrence. Then we make use of five Variables (newly introduced with FM 8). These $Variables only live for the time the script is running.

$n will hold the number of repetitions (please specify)$i, $j are loop counters and$tmp1, $tmp2 hold the values to sort temporarily.

Please note: sorting depends on the field type of TO::array. A text type is sorted like: 1, 10, 11, 2, 3, a, b, ..., and a number type like: 1, 2, 3, 10, 11. Number fields containing no numerical digits will be treated as zero.

If you want to sort more than one Repeating Field at the same time, like having an article list consisting of several colums, simply extend the middle part of the loop and add the other repeating fields as well:

It could be sped up by using QuickSort. The C source is quoted here. Please note: the worst case for this algorithm is an already sorted list! Don't use when an occasional long run is not acceptable. The translation into FileMaker language is left as an exercise to the reader.