def parse_semester(semester: str) -> tuple[int, int]: """ Parses the semester string into a sortable format. Returns a tuple of (year, type), where type is 0 for SoSe and 1 for WiSe. """ if semester.startswith("SoSe"): return int(semester.split()[1]), 0 elif semester.startswith("WiSe"): year_part = semester.split()[1] if "/" in year_part: start_year, _ = map(int, year_part.split("/")) else: start_year = int(year_part) return start_year, 1 else: raise ValueError(f"Invalid semester format: {semester}") def custom_sort(entries) -> list: """ Sorts the list of tuples based on the custom schema. :param entries: List of tuples in the format (str, int, int). :return: Sorted list of tuples. """ return sorted( entries, key=lambda entry: ( parse_semester(entry[0]), # Sort by semester parsed as (year, type) entry[1], # Then by the second element of the tuple entry[2], # Finally by the third element of the tuple ), ) def sort_semesters_list(semesters: list) -> list: """ Sorts a list of semester strings based on year and type. :param semesters: List of semester strings (e.g., "SoSe 24", "WiSe 22/23"). :return: Sorted list of semester strings. """ return sorted(semesters, key=parse_semester) if __name__ == "__main__": unsorted = [ "SoSe 24", "WiSe 22/23", "WiSe 23/24", "WiSe 20/21", "SoSe 23", "SoSe 20", "WiSe 7/8", "WiSe 14/15", "WiSe 13/14", "SoSe 8", "WiSe 19/20", "WiSe 12/13", "WiSe 21/22", "WiSe 18/19", "WiSe 11/12", "WiSe 9/10", "WiSe 6/7", "SoSe 7", "WiSe 16/17", "WiSe 24/25", "SoSe 25", ] # print(sort_semesters_list(unsorted))