Next: , Previous: %auto, Up: Top


12 %sort

This special field allows to set a sorting criteria for the records contained in a record set. Its usage is:

     %sort: field_name

Meaning that the desired order for the records will be determined by the contents of the fields named field_name. The sorting is always done in ascending order, and there may be records not featuring fields named field_name, i.e. it is not mandatory for the sorting field to be mandatory :D.

It is an error to have more than one %sort field in the same record descriptor, as only one field can be used as sorting criteria.

Consider for example that we want to keep the records in our stockage system ordered by entry date. We could achieve that by using the following record descriptor in the database:

     %rec: Item
     %type: Date date
     %sort: Date
     
     Id: 1
     Title: Staplers
     Date: 10 February 2011
     
     Id: 2
     Title: Ruler Pack 20
     Date: 2 March 2009
     
     ...

As you can see in the example above, the fact we use %sort in a database does not mean that the database will be always physically ordered. Unsorted record sets are not considered a data integrity problem, and thus the diagnosis tools must not declare a recfile as invalid because of this. Some tool may provide a way to physically order the fields in the file. That is certainly the case of the GNU recutils, where recfix can be used for that purpose. See recfix.

On the other hand any program listing, presenting or processing data extracted from the recfile must honour the %sort entry. For example, when using the following recsel program in the database above we would get the output sorted by date:

     $ recsel stockage.rec
     Id: 2
     Title: Ruler Pack 20
     Date: 2 March 2009
     
     Id: 1
     Title: Staplers
     Date: 10 February 2011

The sorting of the selected field depends on its type: