array_sort_desc()
Applies to: ✅ Microsoft Fabric ✅ Azure Data Explorer ✅ Azure Monitor ✅ Microsoft Sentinel
Receives one or more arrays. Sorts the first array in descending order. Orders the remaining arrays to match the reordered first array.
Syntax
array_sort_desc(
array1[, ..., argumentN])
array_sort_desc(
array1[, ..., argumentN],
nulls_last)
If nulls_last isn't provided, a default value of true
is used.
Learn more about syntax conventions.
Parameters
Name | Type | Required | Description |
---|---|---|---|
array1...arrayN | dynamic |
✔️ | The array or list of arrays to sort. |
nulls_last | bool |
Determines whether null s should be last. |
Returns
Returns the same number of arrays as in the input, with the first array sorted in ascending order, and the remaining arrays ordered to match the reordered first array.
null
is returned for every array that differs in length from the first one.
An array which contains elements of different types, is sorted in the following order:
- Numeric,
datetime
, andtimespan
elements - String elements
- Guid elements
- All other elements
Examples
The examples in this section show how to use the syntax to help you get started.
Sort two arrays
The following example sorts the initial array, array1
, in descending order. It then sorts array2
to match the new order of array1
.
let array1 = dynamic([1,3,4,5,2]);
let array2 = dynamic(["a","b","c","d","e"]);
print array_sort_desc(array1,array2)
Output
array1_sorted | array2_sorted |
---|---|
[5,4,3,2,1] | ["d","c","b","e","a"] |
Note
The output column names are generated automatically, based on the arguments to the function. To assign different names to the output columns, use the following syntax: ... | extend (out1, out2) = array_sort_desc(array1,array2)
.
Sort substrings
The following example sorts a list of names in descending order. It saves a list of names to a variable, Names
, which is then splits into an array and sorted in descending order. The query returns the names in descending order.
let Names = "John,Paul,Jane,Kayo";
let SortedNames = strcat_array(array_sort_desc(split(Names, ",")), ",");
print result = SortedNames
Output
result |
---|
Paul,Kayo,John,Jane |
Combine summarize and array_sort_desc
The following example uses the summarize
operator and the array_sort_asc
function to organize and sort commands by user in descending chronological order.
datatable(command:string, command_time:datetime, user_id:string)
[
'chmod', datetime(2019-07-15), "user1",
'ls', datetime(2019-07-02), "user1",
'dir', datetime(2019-07-22), "user1",
'mkdir', datetime(2019-07-14), "user1",
'rm', datetime(2019-07-27), "user1",
'pwd', datetime(2019-07-25), "user1",
'rm', datetime(2019-07-23), "user2",
'pwd', datetime(2019-07-25), "user2",
]
| summarize timestamps = make_list(command_time), commands = make_list(command) by user_id
| project user_id, commands_in_chronological_order = array_sort_desc(timestamps, commands)[1]
Output
user_id | commands_in_chronological_order |
---|---|
user1 | [ "rm", "pwd", "dir", "chmod", "mkdir", "ls" ] |
user2 | [ "pwd", "rm" ] |
Note
If your data can contain null
values, use make_list_with_nulls instead of make_list.
Control location of null
values
By default, null
values are put last in the sorted array. However, you can control it explicitly by adding a bool
value as the last argument to array_sort_asc()
.
The following example shows the default behavior:
print result=array_sort_desc(dynamic([null,"blue","yellow","green",null]))
Output
result |
---|
["yellow","green","blue",null,null] |
The following example shows nondefault behavior using the false
parameter, which specifies that nulls are placed at the beginning of the array.
print result=array_sort_desc(dynamic([null,"blue","yellow","green",null]), false)
Output
result |
---|
[null,null,"yellow","green","blue"] |