import json
import re


def parse_key(key):
    parts = key.split("_")
    parsed = []
    for part in parts:
        if part.isdigit():
            parsed.append((0, int(part)))  # 数字优先，按大小
        else:
            parsed.append((1, part))  # 字符串按字典序
    return parsed


def sort_json_keys(input_file, output_file):
    with open(input_file, "r", encoding="utf-8") as f:
        data = json.load(f)

    sorted_items = sorted(data.items(), key=lambda item: parse_key(item[0]))

    sorted_data = {k: v for k, v in sorted_items}

    with open(output_file, "w", encoding="utf-8") as f:
        json.dump(sorted_data, f, ensure_ascii=False, indent=2)


# 示例用法
if __name__ == "__main__":
    sort_json_keys("text_original/ch2/en2.json", "text_original/ch2/3n2.json")
