Skip to content

Commit

Permalink
Fixed natural sort
Browse files Browse the repository at this point in the history
  • Loading branch information
RhetTbull committed Feb 1, 2024
1 parent 522c573 commit a585cc9
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion examples/sort_by_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,34 @@ def sort_by_name(dry_run, start, step):
):
raise click.Abort()

photos = sorted(photos, key=lambda photo: photo.filename)
photos = natural_sort(photos)
for photo in photos:
click.echo(f"Setting date for {photo.filename} to {start}")
if not dry_run:
photo.date = start
start += step


def natural_sort(input_list: list[str]) -> list[str]:
"""Sort the given list in the way that humans expect."""
import re

def convert(text: str) -> list[int | str]:
return [int(text)] if text.isdigit() else [text.lower()]

def alphanum_key(key: str) -> list[int | str]:
return [convert(c) for c in re.split("([0-9]+)", key)]

return sorted(input_list, key=alphanum_key)


def validate_date(photo: photoscript.Photo) -> datetime.datetime:
"""Verify a datetime is valid and if not return today's date."""
try:
return photo.date
except Exception as e:
return datetime.datetime.now()


if __name__ == "__main__":
sort_by_name()

0 comments on commit a585cc9

Please sign in to comment.