# sort

When this function is used on a list or tuple, a new sorted list is returned.

This function does not generate a change.

### Function

array.`sort([closure, [reverse]])`

### Arguments

Argument Type Description
callback closure (optional) Closure to execute on each value, which is used to determine how the list should be sorted.
reverse bool (optional) Determines the order of the returned list.

### Return value

A new sorted list.

### Examples

Simple sort

``````// return a new list in ascending order
[3, 1, 2].sort();
``````

Return value in JSON format

``````[
1,
2,
3
]
``````

With the `reverse` argument set to `true`, a new list will be returned in descending order.

Reverse sort

``````// return a new list in descending order
[3, 1, 2].sort(true);
``````

Return value in JSON format

``````[
3,
2,
1
]
``````

When using a closure the sort behavior depends on the number of arguments the closure accepts. With one argument, each item in the list will be passed to the closure and the return value of the closure will be used to compare. Below is an example which uses such a closure to do a case-insensitive sort. Another common use case is when you want to sort things based on a property. For example a closure like `|u| u.age` could be used to sort users based on an `age` property.

Sort using an one-argument callback

``````// return a new list in ascending order, case-insensitive
["charlie", "alpha", "Beta"].sort(|s| s.lower());
``````

Return value in JSON format

``````[
"alpha",
"Beta",
"charlie"
]
``````

As a second option the sort function can accept a closure with two arguments. The return value of the given closure must return an integer value. When this value is `< 0` then `a` goes before `b`, when `0` then both `a` and `b` are considered equal, and when `>0` then `a` goes after `b`.

Sort using a two-argument callback

``````// list with `nil` values
arr = [nil, "charlie", nil, "alpha", "beta"];

// return a new list in ascending order with `nil` values at the end
arr.sort(|a, b| is_nil(a) ? 1 : is_nil(b) ? -1 : a < b ? -1 : a > b ? 1 : 0);
``````

Return value in JSON format

``````[
"alpha",
"beta",
"charlie",
null,
null
]
``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.6299908,"math_prob":0.7387028,"size":2129,"snap":"2022-27-2022-33","text_gpt3_token_len":562,"char_repetition_ratio":0.14211765,"word_repetition_ratio":0.065104164,"special_character_ratio":0.27148896,"punctuation_ratio":0.1463964,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9620817,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-08-11T05:55:55Z\",\"WARC-Record-ID\":\"<urn:uuid:a1401357-0d65-4fe6-a31d-448e4244c769>\",\"Content-Length\":\"58412\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b2bb8209-244d-47ae-a01a-0391075c4566>\",\"WARC-Concurrent-To\":\"<urn:uuid:c93ff327-dca2-4148-bdef-cee1d80ea058>\",\"WARC-IP-Address\":\"185.199.111.153\",\"WARC-Target-URI\":\"https://docs.thingsdb.net/v1/data-types/list/sort/\",\"WARC-Payload-Digest\":\"sha1:YT23X5HFI2P6IDWA2R2NKE7LM7AFYQFH\",\"WARC-Block-Digest\":\"sha1:26L2VR6PLS6WMXHEWSFDECMYQBCCLYD3\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-33/CC-MAIN-2022-33_segments_1659882571234.82_warc_CC-MAIN-20220811042804-20220811072804-00415.warc.gz\"}"}