def sort_by(self, *fields, **kwargs):
self._max = kwargs.get('<STR_LIT>', <NUM_LIT:0>)<EOL>if isinstance(fields, (string_types, SortDirection)):<EOL><INDENT>fields = [fields]<EOL><DEDENT>for f in fields:<EOL><INDENT>if isinstance(f, SortDirection):<EOL><INDENT>self._sortby += [f.field, f.DIRSTRING]<EOL><DEDENT>else:<EOL><INDENT>self._sortby.append(f)<EOL><DEDENT><DEDENT>return self<EOL>
Indicate how the results should be sorted. This can also be used for
*top-N* style queries

### Parameters

- **fields**: The fields by which to sort. This can be either a single
    field or a list of fields. If you wish to specify order, you can
    use the `Asc` or `Desc` wrapper classes.
- **max**: Maximum number of results to return. This can be used instead
    of `LIMIT` and is also faster.


Example of sorting by `foo` ascending and `bar` descending:

```
sort_by(Asc('@foo'), Desc('@bar'))
```

Return the top 10 customers:

```
AggregateRequest()\
    .group_by('@customer', r.sum('@paid').alias(FIELDNAME))\
    .sort_by(Desc('@paid'), max=10)
```