1. from datetime import datetime
  2. def sort_timestamps(records):
  3. """Sorts a list of timestamp records.
  4. Args:
  5. records: A list of strings representing timestamps in a consistent format.
  6. Returns:
  7. A list of strings representing the sorted timestamps.
  8. """
  9. # Convert strings to datetime objects for sorting
  10. datetime_objects = [datetime.fromisoformat(record) for record in records]
  11. # Sort the datetime objects
  12. datetime_objects.sort()
  13. # Convert back to strings
  14. sorted_records = [record.strftime('%Y-%m-%d %H:%M:%S') for record, datetime in zip(records, datetime_objects)]
  15. return sorted_records
  16. if __name__ == '__main__':
  17. # Example usage
  18. timestamp_records = [
  19. "2024-01-26 10:00:00",
  20. "2024-01-25 12:30:00",
  21. "2024-01-27 08:45:00",
  22. "2024-01-26 15:15:00"
  23. ]
  24. sorted_timestamps = sort_timestamps(timestamp_records)
  25. print(sorted_timestamps)

Add your comment