Sort the results

Use SQL to sort result rows when you express your queries. Sorted results are useful when you need to see records in some particular order.

The following query sorts a list of cities and their populations in descending order by population:
SELECT name, population FROM cities ORDER BY population desc;

If a DataBlade® module defines a data type that can be sorted in a meaningful way, you must supply a comparison routine for the type. This routine allows the user to sort query results on that type.

In addition, you can use the results of routines that appear in the target list to sort the results of a query. For example, the following query returns a list of cities in descending order by population density:
SELECT name, population, 
   population / Area(boundary) AS density
   FROM cities 
   ORDER BY density desc;

The density expression, on which the query results are sorted, is a complex calculation. The expression includes a DataBlade module routine and a division operation. Because your HCL Informix® database server allows sorting by floating-point numbers, the preceding query requires no special sorting support from the DataBlade module.

To determine whether to provide sorted results, ask the following questions:
  • Can my DataBlade module data types be sorted?
  • Will users want to sort this data type?