---
name: "BIS_fnc_sortBy"
type: function
status: ok
groups: ["Arrays"]
arma3_since: "0.58"
locality: {"argument": null, "effect": null, "server_exec": false}
source: https://community.bistudio.com/wiki/BIS_fnc_sortBy
revid: 327964
updated: 2022-07-13T13:57:43Z
---

# BIS_fnc_sortBy

*Function · Arrays · Arma 3 v0.58*

## Description

Sorts an array according to given algorithm. See [sort](../commands/sort.md) for simple sorts.

## Syntax

```sqf
[array, parameters, algorithm, direction, filter] call BIS_fnc_sortBy
```

**Parameters:**

- `array`: [Array](../topics/Array.md) - any unsorted array. Can contain any types
- `parameters`: [Array](../topics/Array.md)
  - used in the eval algorithm
  - input params are referenced in the sorting algorithm by _input0..9
  - a maximum of 10 input params is supported (0-9)
- `algorithm`: [Code](../topics/Code.md) - (Optional, default `{ _x }`)
  - needs to return [String](../topics/String.md), [Number](../topics/Number.md) or [Array](../topics/Array.md) - see [sort](../commands/sort.md)
  - [_x](../topics/Magic_Variables.md#x) refers to array item
- `direction`: [String](../topics/String.md) - (Optional, default "ASCEND") case-insensitive
  - "ASCEND" for ascending sorting
  - "DESCEND" (or anything else) for descending sorting
- `filter`: [Code](../topics/Code.md) - (Optional, default `{ true }`) code that needs to evaluate true for the array item to be sorted, otherwise item is removed

**Returns:** [Array](../topics/Array.md) - sorted array

## Examples

### Example 1

```sqf
// sort numbers from lowest to highest
_sortedNumbers = [[1,-80,0,480,15,-40], [], {_x}, "ASCEND"] call BIS_fnc_sortBy;
```

### Example 2

```sqf
// sort helicopters by distance from player
_closestHelicopters = [[_heli1,_heli2,_heli3], [], { player distance _x }, "ASCEND"] call BIS_fnc_sortBy;
```

### Example 3

```sqf
// sort enemy by distance from friendly unit (referenced by local variable), the furthest first
_furtherstEnemy = [[_enemy1,_enemy2,_enemy3], [_friendly], { _input0 distance _x }, "DESCEND", { canMove _x }] call BIS_fnc_sortBy;
```

## See also

[BIS_fnc_sortAlphabetically](../functions/BIS_fnc_sortAlphabetically.md) · [BIS_fnc_sortNum](../functions/BIS_fnc_sortNum.md) · [sort](../commands/sort.md)

<sub>Source: [BI Community Wiki — BIS fnc sortBy](https://community.bistudio.com/wiki/BIS_fnc_sortBy) (revision 327964).</sub>
