array_sort_asc()array_sort_asc()
接收一个或多个数组。Receives one or more arrays. 按升序对第一个数组进行排序。Sorts the first array in ascending order. 对其余数组进行排序,以匹配重新排序的第一个数组。Orders the remaining arrays to match the reordered first array.
语法Syntax
array_sort_asc(
array1 [, ..., argumentN ])
array_sort_asc(
array1 [, ..., argumentN ])
array_sort_asc(
array1 [, ..., argumentN ],
nulls_last)
array_sort_asc(
array1 [, ..., argumentN ],
nulls_last)
如果未提供 nulls_last,则使用默认值 true
。If nulls_last isn't provided, a default value of true
is used.
参数Arguments
- array1...arrayN:输入数组。array1...arrayN : Input arrays.
- nulls_last:一个布尔值,指示
null
是否应位于最后nulls_last : A bool indicating whethernull
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
。null
will be returned for every array that differs in length from the first one.
如果数组包含不同类型的元素,则按以下顺序对其排序:If an array contains elements of different types, it will be sorted in the following order:
- 数值、
datetime
和timespan
元素Numeric,datetime
, andtimespan
elements - 字符串元素String elements
- GUID 元素Guid elements
- 所有其他元素All other elements
示例 1 - 对两个数组排序Example 1 - Sorting two arrays
let array1 = dynamic([1,3,4,5,2]);
let array2 = dynamic(["a","b","c","d","e"]);
print array_sort_asc(array1,array2)
array1_sorted |
array2_sorted |
---|---|
[1,2,3,4,5][1,2,3,4,5] | ["a","e","b","c","d"]["a","e","b","c","d"] |
备注
输出列名称根据函数的参数自动生成。The output column names are generated automatically, based on the arguments to the function. 若要为输出列分配不同的名称,请使用以下语法:... | extend (out1, out2) = array_sort_asc(array1,array2)
To assign different names to the output columns, use the following syntax: ... | extend (out1, out2) = array_sort_asc(array1,array2)
示例 2 - 对子字符串排序Example 2 - Sorting substrings
let Names = "John,Paul,George,Ringo";
let SortedNames = strcat_array(array_sort_asc(split(Names, ",")), ",");
print result = SortedNames
result |
---|
George,John,Paul,RingoGeorge,John,Paul,Ringo |
示例 3 - 合并 summarize 和 array_sort_ascExample 3 - Combining summarize and array_sort_asc
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_asc(timestamps, commands)[1]
user_id |
commands_in_chronological_order |
---|---|
user1user1 | [[ "ls","ls", "mkdir","mkdir", "chmod","chmod", "dir","dir", "pwd","pwd", "rm""rm" ]] |
user2user2 | [[ "rm","rm", "pwd""pwd" ]] |
备注
如果数据可能包含 null
值,请使用 make_list_with_nulls,而不是 make_list。If your data may contain null
values, use make_list_with_nulls instead of make_list.
示例 4 - 控制 null
值的位置Example 4 - Controlling location of null
values
默认情况下,null
值放在已排序数组的最后。By default, null
values are put last in the sorted array. 但是,你可以显式控制它,方法是将 bool
值添加为 array_sort_asc()
的最后一个参数。However, you can control it explicitly by adding a bool
value as the last argument to array_sort_asc()
.
具有默认行为的示例:Example with default behavior:
print array_sort_asc(dynamic([null,"blue","yellow","green",null]))
print_0 |
---|
["blue","green","yellow",null,null]["blue","green","yellow",null,null] |
具有非默认行为的示例:Example with non-default behavior:
print array_sort_asc(dynamic([null,"blue","yellow","green",null]), false)
print_0 |
---|
[null,null,"blue","green","yellow"][null,null,"blue","green","yellow"] |
请参阅See also
若要按降序对第一个数组排序,请使用 array_sort_desc()。To sort the first array in descending order, use array_sort_desc().