SORT_BY_KEY
Sort a list of objects by a key

Description

Sorts an array of objects by the values of the given key.
If the objects do not contain text or number values under the given key, the sort might not behave as expected.
The third parameter can be used to sort in ascending (default) or descending order.
Compared to SORT\_BY\_KEY\_CASE\_SENSITIVE, this function is case insensitive.

Function signature

1
SORT_BY_KEY(list: Array, key: String, order: String = asc) => Array
Copied!
Parameter
Description
List
List to sort
Key
Key whose value to sort by
Order
Order to sort in ("asc" for ascending, "desc" for descending); defaults to ascending

Examples

1
array = [
2
{
3
"name": "Eric",
4
"year": 2010
5
},
6
{
7
"name": "Jane",
8
"year": 1987
9
},
10
{
11
"name": "Karen",
12
"year": 1980
13
},
14
{
15
"name": "John",
16
"year": 2005
17
}
18
]
Copied!
Formula
Return value
SORT_BY_KEY(array, "year")
[{"name":"Karen","year":1980},{"name":"Jane","year":1987},{"name":"John","year":2005},{"name":"Eric","year":2010}]
SORT_BY_KEY(array, "year", "desc")
[{"name":"Eric","year":2010},{"name":"John","year":2005},{"name":"Jane","year":1987},{"name":"Karen","year":1980}]
SORT_BY_KEY(array, "name", "asc")
[{"name":"Eric","year":2010},{"name":"Jane","year":1987},{"name":"John","year":2005},{"name":"Karen","year":1980}]
Last modified 1yr ago
Export as PDF
Copy link