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
(self, *fields: str, **kwargs)
| 224 | return self |
| 225 | |
| 226 | def sort_by(self, *fields: str, **kwargs) -> "AggregateRequest": |
| 227 | """ |
| 228 | Indicate how the results should be sorted. This can also be used for |
| 229 | *top-N* style queries |
| 230 | |
| 231 | ### Parameters |
| 232 | |
| 233 | - **fields**: The fields by which to sort. This can be either a single |
| 234 | field or a list of fields. If you wish to specify order, you can |
| 235 | use the `Asc` or `Desc` wrapper classes. |
| 236 | - **max**: Maximum number of results to return. This can be |
| 237 | used instead of `LIMIT` and is also faster. |
| 238 | |
| 239 | |
| 240 | Example of sorting by `foo` ascending and `bar` descending: |
| 241 | |
| 242 | ``` |
| 243 | sort_by(Asc("@foo"), Desc("@bar")) |
| 244 | ``` |
| 245 | |
| 246 | Return the top 10 customers: |
| 247 | |
| 248 | ``` |
| 249 | AggregateRequest()\ |
| 250 | .group_by("@customer", r.sum("@paid").alias(FIELDNAME))\ |
| 251 | .sort_by(Desc("@paid"), max=10) |
| 252 | ``` |
| 253 | """ |
| 254 | |
| 255 | fields_args = [] |
| 256 | for f in fields: |
| 257 | if isinstance(f, (Asc, Desc)): |
| 258 | fields_args += [f.field, f.DIRSTRING] |
| 259 | else: |
| 260 | fields_args += [f] |
| 261 | |
| 262 | ret = ["SORTBY", str(len(fields_args))] |
| 263 | ret.extend(fields_args) |
| 264 | max = kwargs.get("max", 0) |
| 265 | if max > 0: |
| 266 | ret += ["MAX", str(max)] |
| 267 | |
| 268 | self._aggregateplan.extend(ret) |
| 269 | return self |
| 270 | |
| 271 | def filter(self, expressions: Union[str, List[str]]) -> "AggregateRequest": |
| 272 | """ |