But actually I am a bit disapointed of the capability of Freepascal. In other languages there are predefined functions for such matters that are reliable, easy to use and well and brief documented.
No beating around the bush Freepascal is relatively late. While some basics were always covered ( e.g. for strings, one can simple use tstringlist, with custom sorting sorting, and there is a generalized qsort in the FPC examples and many circulate on the web).
And SQL-like sorting using custom datasets (like Mike Cornflake says) already works for ages , because there sorting rules are more fixed
For sorts parameterized with functions, you need two (sort and compare, and having generics eliminates the swap, but that only emerged late.
Note that such features are different for dynamically typed languages like PHP than for statically compiled languages like FPC. PHP is an interpreter, and its datastructures are more interpreter like, and aligned with SQL thinking.
FPC's datastructures are not, and thus database components are a more closer relative to PHP there than "normal" pascal datastructures, which are a level more lowlevel. So if you just want normal database like operation, use datasets.