# Sort

## Excel

Select a range of cells > go to the **Home** tab > **Editing** > **Sort & Filter** and then select sorting options.

## Tableau

Rank can be added as a table calculation or with the syntax: \
&#x20;\~RANK!\~(‹field› «, 'direction'») where «direction» can be either `asc` or `desc`.

```
RANK([Country], 'asc')
RANK([Salary], 'desc')
```

## Alteryx

Use the **Sort** tool (in the **Preparation** palette).\
\
&#x20;![](https://3800670130-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4uObfHWvsn0aAfuifV%2F-M4uOfElaTa53MyrD8KZ%2F-M4uOvJ1kwFSRrtGlAF3%2FSort%20Tool.PNG?generation=1586896554414087\&alt=media)

(and select the *Sort* and *Direction* values).

## OrgVue

In OrgVue, strings and numbers are sorted differently. \
&#x20;**Sorting strings:** \
&#x20;array(‹string\_array›).\~sort!\~('item'«, 'direction'») \
&#x20;OR \
&#x20;‹collection›.\~sort!\~('‹dimension›'«, 'direction'»)

```
array([2,1,'b','c','a']).sort('item', 'desc'); // returns c,b,a,2,1
// OR
node.d.sort('department', 'asc');
```

NB.:

* If no direction is supplied, an ascending order is applied by default.&#x20;
* If the values being sorted are of mixed type, `sort()` lists numbers in ascending order and then sorts strings alphabetically (which can be considered 'ascending order').

**Sorting numbers:**

It is possible to apply the above syntax when sorting a measure. However, it will treat the measure as if it were a string, e.g. sorting 9.9 -> 800 -> 70 -> ... To correctly sort numerical fields, use: ‹collection›.\~sort!\~('‹measure›', \~sort.number!\~|sort.‹direction›)

```
node.c.sort('current_salary', sort.number|sort.desc)
```

## tSQL

\~ORDER BY!\~ ‹field› \[‹direction›]

```
SELECT * 
FROM EmployeeData
ORDER BY CurrentSalary [DESC]
```

## Python

\~sorted!\~(‹arr›, ‹key=lambda n: n.name›)

```
sorted(arr, key=lambda n: n.name)
```

This assumes that the variable `arr` is an array containing objects. If you want to sort a simple array of strings, you can use: \
&#x20;\~sorted!\~(‹array›, «reverse=True») \
&#x20;where the inclusion of `reverse=True` reverses the sort order:

```
sorted(['2','1','b','c','a']) 
# Returns ['1', '2', 'a', 'b', 'c']

sorted(['2','1','b','c','a'], reverse=True) 
# Returns ['c', 'b' , 'a', '2', '1']
```

NB.:

* An ascending order is applied by default.&#x20;
* If the values being sorted are of mixed type, `sorted()` lists numbers in ascending order and then sorts strings alphabetically (which can be considered 'ascending order').&#x20;
* All numbers must be wrapped in quotes.
