-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsort_by.yml
More file actions
80 lines (78 loc) · 3.09 KB
/
sort_by.yml
File metadata and controls
80 lines (78 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
name: sort_by
topic: collections
args:
required:
- name: elements
type: array
- name: expr
type: [expression->number, expression->string]
returns:
type: array
desc: |
Sort an array using an expression `expr` as the sort key. For each element
in the array of `elements`, the `expr` expression is applied and the
resulting value is used as the key used when sorting the `elements`.
If the result of evaluating the `expr` against the current array element
results in type other than a `number` or a `string`, a type error will
occur.
Below are several examples using the `people` array (defined above) as the
given input. `sort_by` follows the same sorting logic as the `sort`
function.
examples:
test3:
context: &data
people: [{age: 20, age_str: '20', bool: true, name: a, extra: foo}, {age: 40,
age_str: '40', bool: false, name: b, extra: bar}, {age: 30, age_str: '30',
bool: true, name: c}, {age: 50, age_str: '50', bool: false, name: d}, {
age: 10, age_str: '10', bool: true, name: 3}]
empty_list: []
args: [people, '&age']
returns: [{age: 10, age_str: '10', bool: true, name: 3}, {age: 20, age_str: '20',
bool: true, name: a, extra: foo}, {age: 30, age_str: '30', bool: true, name: c},
{age: 40, age_str: '40', bool: false, name: b, extra: bar}, {age: 50, age_str: '50',
bool: false, name: d}]
test4:
context: *data
args: [people, '&age_str']
returns: [{age: 10, age_str: '10', bool: true, name: 3}, {age: 20, age_str: '20',
bool: true, name: a, extra: foo}, {age: 30, age_str: '30', bool: true, name: c},
{age: 40, age_str: '40', bool: false, name: b, extra: bar}, {age: 50, age_str: '50',
bool: false, name: d}]
test5:
context: *data
args: [people, '&to_number(age_str)']
returns: [{age: 10, age_str: '10', bool: true, name: 3}, {age: 20, age_str: '20',
bool: true, name: a, extra: foo}, {age: 30, age_str: '30', bool: true, name: c},
{age: 40, age_str: '40', bool: false, name: b, extra: bar}, {age: 50, age_str: '50',
bool: false, name: d}]
test7:
context: *data
args: [people, '&extra']
error: invalid-type
test8:
context: *data
args: [people, '&bool']
error: invalid-type
test9:
context: *data
args: [people, '&name']
error: invalid-type
test10:
context: *data
args: [people, name]
error: invalid-type
test12:
context: *data
args: [empty_list, '&age']
returns: []
test13:
context:
people: [{age: 10, order: '1'}, {age: 10, order: '2'}, {age: 10, order: '3'},
{age: 10, order: '4'}, {age: 10, order: '5'}, {age: 10, order: '6'}, {age: 10,
order: '7'}, {age: 10, order: '8'}, {age: 10, order: '9'}, {age: 10, order: '10'},
{age: 10, order: '11'}]
args: [people, '&age']
returns: [{age: 10, order: '1'}, {age: 10, order: '2'}, {age: 10, order: '3'},
{age: 10, order: '4'}, {age: 10, order: '5'}, {age: 10, order: '6'}, {age: 10,
order: '7'}, {age: 10, order: '8'}, {age: 10, order: '9'}, {age: 10, order: '10'},
{age: 10, order: '11'}]