File size: 1,184 Bytes
294d395
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import List, TypedDict
from datetime import datetime

class TaskItem(TypedDict):
    task_id: str
    priority: str  # low, normal, high, urgent
    deadline_local: str  # ISO-like datetime string

PRIORITY_ORDER = {
    "urgent": 0,
    "high": 1,
    "normal": 2,
    "low": 3,
}

def _priority_key(task: TaskItem):
    pr = task.get("priority", "normal").lower()
    pr_rank = PRIORITY_ORDER.get(pr, 2)
    try:
        dt = datetime.fromisoformat(task.get("deadline_local", ""))
    except Exception:
        dt = datetime.max
    return (pr_rank, dt)

def sort_tasks(tasks: List[TaskItem]) -> List[TaskItem]:
    """Sort tasks by priority and deadline, highest priority first."""
    return sorted(tasks, key=_priority_key)


if __name__ == "__main__":
    sample = [
        {"task_id": "T1", "priority": "normal", "deadline_local": "2025-01-14 21:00"},
        {"task_id": "T2", "priority": "urgent", "deadline_local": "2025-01-14 10:00"},
        {"task_id": "T3", "priority": "high", "deadline_local": "2025-01-14 09:30"},
        {"task_id": "T4", "priority": "low", "deadline_local": "2025-01-16 09:00"},
    ]
    for t in sort_tasks(sample):
        print(t)