-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_json_sorting.py
More file actions
66 lines (61 loc) · 1.62 KB
/
Copy pathcheck_json_sorting.py
File metadata and controls
66 lines (61 loc) · 1.62 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
import json
def is_sorted_by_field(json_list, field, descending):
for i in range(1, len(json_list)):
if descending:
if json_list[i - 1][field] < json_list[i][field]:
return False
else:
if json_list[i - 1][field] > json_list[i][field]:
return False
return True
# Sample JSON data
json_data = '''
{
"category": "dsp_id",
"currency": "USD",
"data": [
{
"recommendation": 20056200,
"filtering_rate": 0.8071,
"imps": 632,
"reduced_imps": 0.2518,
"media_cost": 7.18,
"reduced_media_cost": 0.1975,
"category_value": "37921"
},
{
"recommendation": 34110000,
"filtering_rate": 0.8038,
"imps": 690,
"reduced_imps": -0.0049,
"media_cost": 26.37,
"reduced_media_cost": 0.0583,
"category_value": "951"
},
{
"recommendation": 84700200,
"filtering_rate": -0.0064,
"imps": 73199,
"reduced_imps": -0.02,
"media_cost": 978.32,
"reduced_media_cost": -0.0059,
"category_value": "892"
}
],
"total": {
"recommendation": 1983568500,
"filtering_rate": 0.5054,
"imps": 600010,
"reduced_imps": 0.0163,
"media_cost": 4695.34,
"reduced_media_cost": 0.0039
}
}
'''
# Parse the JSON data
parsed_data = json.loads(json_data)
# Extract the "data" field
data_list = parsed_data["data"]
# Check if the list is sorted by the "filtering_rate" field in descending order
is_sorted = is_sorted_by_field(data_list, "filtering_rate", descending=True)
print("Is the list sorted by 'filtering_rate' field (descending)?", is_sorted)