Ordering the results of a simple query

Relational databases can return result sets with rows in a seemingly random order. With SQL, the ORDER BY clause lets you specify how the database sorts fetched data. The following SQL statement sorts the result set on the postal codes of the addresses.

SELECT * FROM addresses
  WHERE city = 'Chicago'
  ORDER BY postal_code;

The following Gosu code sorts the instances in the result object in the same way as the preceding SQL example.

uses gw.api.database.Query 
uses gw.api.database.QuerySelectColumns // Import the class that defines a column
uses gw.api.path.Paths                  // Import the class that builds a path to a column

var query = Query.make(Address)
query.compare(Address#City, Equals, "Chicago")
var select = query.select()
// Specify to sort the result by postal code.
select.orderBy(QuerySelectColumns.path(Paths.make(Address#PostalCode)))

See also