-
Notifications
You must be signed in to change notification settings - Fork 349
Expand file tree
/
Copy pathsort_forms.py
More file actions
66 lines (48 loc) · 1.49 KB
/
sort_forms.py
File metadata and controls
66 lines (48 loc) · 1.49 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
import re
from pathlib import Path
def sort_key(key):
"""
Sort order:
1. Main numeric part
2. No suffix
3. Numeric suffix (_2)
4. Letter suffix (668m, 876f)
5. Other suffixes (_g, etc.)
"""
key = str(key)
match = re.match(r"^(\d+)", key)
if not match:
return (float("inf"), 4, key)
main_number = int(match.group(1))
rest = key[len(match.group(1)) :]
if rest == "":
return (main_number, 0, 0)
if rest.startswith("_"):
suffix = rest[1:]
if suffix.isdigit():
return (main_number, 1, int(suffix))
else:
return (main_number, 3, suffix)
if rest.isalpha():
return (main_number, 2, rest)
return (main_number, 4, rest)
def main():
# Get directory where this script lives
script_dir = Path(__file__).resolve().parent
# Build absolute path to scripts/forms.json
json_path = script_dir / "forms.json"
if not json_path.exists():
raise FileNotFoundError(f"Could not find file at: {json_path}")
# Load JSON
with open(json_path, "r", encoding="utf-8") as f:
data = json.load(f)
# Sort
sorted_items = sorted(data.items(), key=lambda x: sort_key(x[0]))
sorted_dict = dict(sorted_items)
# Overwrite same file
with open(json_path, "w", encoding="utf-8") as f:
json.dump(sorted_dict, f, indent=4, ensure_ascii=False)
print(f"{json_path} sorted successfully.")
if __name__ == "__main__":
main()