# QBSorts

## Overview

The `QBSorts` class is a versatile utility for managing sorting conditions in `QBSelect` queries. It enables developers to define ascending or descending sort orders for columns and functions, offering granular control over query result organization. This class integrates with other query builder components, allowing for dynamic updates to sorting criteria during query construction.

With support for adding primary key columns automatically in alphabetical order, `QBSorts` streamlines the sorting process in complex queries. Additionally, it includes features for clearing sorting conditions, ensuring flexibility and adaptability. Key methods include `add(columnSortAsc)` for adding sort orders, `addPk()` for incorporating primary key columns, and `clear()` for resetting the sort configuration.

For more details, see the [QBSelect documentation](https://docs.servoy.com/reference/servoycore/dev-api/database-manager/qbselect).

## Properties Summarized

| Type                                                                   | Name              | Summary                   |
| ---------------------------------------------------------------------- | ----------------- | ------------------------- |
| [QBSelect](/reference/servoycore/dev-api/database-manager/qbselect.md) | [parent](#parent) |                           |
| [QBSelect](/reference/servoycore/dev-api/database-manager/qbselect.md) | [root](#root)     | Get query builder parent. |

## Methods Summarized

| Type                                                                 | Name                                     | Summary                                                                     |
| -------------------------------------------------------------------- | ---------------------------------------- | --------------------------------------------------------------------------- |
| [QBSorts](/reference/servoycore/dev-api/database-manager/qbsorts.md) | [add(columnSortAsc)](#add-columnsortasc) | Add an ascending sorting on a column to the query sort.                     |
| [QBSorts](/reference/servoycore/dev-api/database-manager/qbsorts.md) | [add(sort)](#add-sort)                   | Add a sorting on a column to the query sort.                                |
| [QBSorts](/reference/servoycore/dev-api/database-manager/qbsorts.md) | [addPk()](#addpk)                        | Add the tables' primary pk columns in alphabetical order to the query sort. |
| [QBSorts](/reference/servoycore/dev-api/database-manager/qbsorts.md) | [clear()](#clear)                        | Clear the sorting clause for the query.                                     |

## Properties Detailed

### parent

**Type**\
[QBSelect](/reference/servoycore/dev-api/database-manager/qbselect.md) the parent QBSelect query associated with these sorts.

### root

Get query builder parent.

**Type**\
[QBSelect](/reference/servoycore/dev-api/database-manager/qbselect.md)

**Sample**

```js
var subquery = datasources.db.example_data.order_details.createSelect();

	var query = datasources.db.example_data.orders.createSelect();
	query.where.add(query
		.or
			.add(query.columns.order_id.not.isin([1, 2, 3]))

			.add(query.exists(
					subquery.where.add(subquery.columns.orderid.eq(query.columns.order_id)).root
			))
		)

	foundset.loadRecords(query)
```

## Methods Detailed

### add(columnSortAsc)

Add an ascending sorting on a column to the query sort.

**Parameters**

* [QBColumn](/reference/servoycore/dev-api/database-manager/qbcolumn.md) **columnSortAsc** column to sort by

**Returns:** [QBSorts](/reference/servoycore/dev-api/database-manager/qbsorts.md) this QBSorts instance after adding the specified column to the sort order.

**Sample**

```js
query.sort.add(query.columns.orderid)
```

### add(sort)

Add a sorting on a column to the query sort.

**Parameters**

* [QBSort](/reference/servoycore/dev-api/database-manager/qbsort.md) **sort** the sort to add

**Returns:** [QBSorts](/reference/servoycore/dev-api/database-manager/qbsorts.md) this QBSorts instance after adding the specified sort condition.

**Sample**

```js
query.sort.add(query.columns.orderid.desc)
```

### addPk()

Add the tables' primary pk columns in alphabetical order to the query sort.

**Returns:** [QBSorts](/reference/servoycore/dev-api/database-manager/qbsorts.md) this QBSorts instance after adding primary key columns to the sort order.

**Sample**

```js
query.sort.addPk()
```

### clear()

Clear the sorting clause for the query.

**Returns:** [QBSorts](/reference/servoycore/dev-api/database-manager/qbsorts.md) this QBSorts instance after clearing all sort conditions.

**Sample**

```js
query.sort.clear()
```

***


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.servoy.com/reference/servoycore/dev-api/database-manager/qbsorts.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
