# Sort Transformation

The *Sort* Transformation object in Astera Data Stack is used to sort an incoming data stream. It also provides the option to remove duplicate values from the input.

It is a blocking transformation which means that input records are accumulated until the end of input. Blocking transformations affect the performance of overall dataflow because subsequent steps cannot be executed until all the records have been received and processed by the blocking transformation.

The *Sort* Transformation uses storage on the server for temporary data during sorting. The server must have enough capacity to store the entire data set and index.

## **Use Case**

We have retrieved the *OrderDetails* data from a database table. The dataset contains fields such as *OrderID*, *ProductID*, UnitPrice, *Quantity*, and *Discount*. This data is unsorted and we want to sort it in the ascending order of *UnitPrice*.

## **Using the Sort Transformation**

1. Drag the *Sort* Transformation object from the *Transformations* section in the *Toolbox* and drop it on the dataflow designer.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2FEERzbFzeSsRKvBEKazoG%2Fimage.png?alt=media&#x26;token=9aaf9993-f07c-48d9-be74-4f0d6fc24af8" alt=""><figcaption></figcaption></figure>

2. Map fields from the source object to the *Sort* Transformation object.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2FpHea1hfJzS86qAkVUfX2%2FSort%20Transformation%20Gif%201.gif?alt=media&#x26;token=2843aaa9-b18d-443f-b008-ddf2542cb5ec" alt=""><figcaption></figcaption></figure>

3. To configure the properties of the *Sort* Transformation object, right-click on its header and select *Properties* from the context menu.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2F1G1X4W0d69EZEpSYTA2M%2Fimage.png?alt=media&#x26;token=b6e7aa2a-49b0-4a4e-9580-8f5ae59d18a3" alt=""><figcaption></figcaption></figure>

4. A *Layout Builder* window will appear.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2FGAgkMFL549Gh3z00nisx%2Fimage.png?alt=media&#x26;token=dcd66eeb-30fa-4398-a72f-4d7ea0c31ada" alt=""><figcaption></figcaption></figure>

In this window you can either:

* Add [*Member Objects or Collection Objects*](https://documentation.astera.com/astera-data-stack-v8/best-practices/overview-of-cardinality-in-data-modeling) to the layout.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2F0Z9wulu67PPhnlJxWfuS%2Fimage.png?alt=media&#x26;token=05774030-aba4-47a7-a76a-e02ea1c9f25e" alt=""><figcaption></figcaption></figure>

* Edit the elements of the *Sort* object. The *Layout Builder* allows you to add or remove fields in the layout, as well as select their data type. The fields added in the *Layout Builder* will be added to the Input node inside the object box. Once you’re done making changes to the layout, click *Next*.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2FWhOtllM9q5eBJvJ6JmcB%2Fimage.png?alt=media&#x26;token=24ba98b0-7047-42cc-af24-06a9f254a2b0" alt=""><figcaption></figcaption></figure>

5. The next window is the *Sort Transformation Properties* window.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2FS3vGMeLEyYeYgFftcznt%2Fimage.png?alt=media&#x26;token=26cb1662-49d8-4fd9-8b30-2a0b852bab52" alt=""><figcaption></figcaption></figure>

Here, you can specify the sorting criteria. You will see the following options on this screen:

* *Return Distinct Values Only*: Check this option if you want to remove duplicate values from the output.
* *Treat Null as the Lowest Value*: Check this option if you want a null value to be returned first in the ascending sort order, and conversely, have the null value returned last in the descending sort order.
* *Case Sensitive*: Check this option if you require a case-sensitive comparison for strings.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2Fq2RctN5oTtoqz3TVNtxf%2Fimage.png?alt=media&#x26;token=69ff2bb1-3958-4da5-9bfd-2dad028aeb0c" alt=""><figcaption></figcaption></figure>

6. On the same screen, you need to select the sorting *Field* from the drop-down list and set the *Sort Order* as *Ascending* or *Descending*.

{% hint style="info" %}
**Note**: In this case, the sorting *Field* is *UnitPrice* and the *Sort Order* is *Ascending*.
{% endhint %}

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2FlMHyvrpzb5xpKamAjazh%2Fimage.png?alt=media&#x26;token=3d9cdaf1-8314-4775-9c22-9a23f12d4cc5" alt=""><figcaption></figcaption></figure>

7. The last window is the *General Options* window. Here you can add *Comments* or specify some *General Options*. Once done, click *OK* and the window will close.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2Fbq6E4ZaRTiA2RXJH2CAE%2Fimage.png?alt=media&#x26;token=ac7f12df-8065-4c52-aca9-7ecec3785c02" alt=""><figcaption></figcaption></figure>

8. You can now map the *Sort* Transformation object to a destination and preview the output.

{% hint style="info" %}
**Note**: In this case, we will write the data to an [*Excel Workbook Destination*](https://documentation.astera.com/v/astera-data-stack-v8/dataflows/destinations/excel-workbook-destination)~~.~~
{% endhint %}

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2FJl9lR6WlL5MkEUX97PDR%2FSort%20Transformation%20Gif%202.gif?alt=media&#x26;token=2dc8e9e3-e4f5-49b5-9834-e380681361ca" alt=""><figcaption></figcaption></figure>

9. The output now shows the entire source data sorted in the ascending order of *UnitPrice*.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2FtBYbCoXCaCPwh48Pc9vj%2Fimage.png?alt=media&#x26;token=abe36be9-a6ce-4765-8721-6f9d1f673135" alt=""><figcaption></figcaption></figure>

This is how the *Sort* Transformation can be used in Astera.
