added count option and sorting for artists
This commit is contained in:
@@ -133,6 +133,33 @@ def sort_events(events: List[Event], sort: Optional[str]) -> List[Event]:
|
||||
return events
|
||||
|
||||
|
||||
def sort_artist_schedules(items, sort: Optional[str]):
|
||||
"""Sort (artist_name, dates) pairs per the ``?sort=`` query param.
|
||||
|
||||
"asc"/"desc" order by each artist's *latest* (most recent) date, since
|
||||
an artist has many dates and not just one to sort on; artists with no
|
||||
parseable date always sort last, regardless of direction. "az"/"za"
|
||||
order alphabetically (case-insensitive) by name. None (or any other
|
||||
value) defaults to "az", matching the plain-list behavior before sort
|
||||
support existed.
|
||||
"""
|
||||
items = list(items)
|
||||
if sort in ("asc", "desc"):
|
||||
def _latest(dates) -> Optional[date]:
|
||||
parsed = [d for d in (parse_event_date(x) for x in dates) if d is not None]
|
||||
return max(parsed) if parsed else None
|
||||
|
||||
with_latest = [(name, dates, _latest(dates)) for name, dates in items]
|
||||
dated = sorted(
|
||||
(item for item in with_latest if item[2] is not None),
|
||||
key=lambda item: item[2],
|
||||
reverse=(sort == "desc"),
|
||||
)
|
||||
undated = [(name, dates) for name, dates, latest in with_latest if latest is None]
|
||||
return [(name, dates) for name, dates, _ in dated] + undated
|
||||
return sorted(items, key=lambda item: item[0].lower(), reverse=(sort == "za"))
|
||||
|
||||
|
||||
def sort_date_strings(dates) -> List[str]:
|
||||
"""Sort raw ``dd.mm.yy`` date strings chronologically, de-duplicated.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user