from datetime import datetime
def sort_timestamps(records):
"""Sorts a list of timestamp records.
Args:
records: A list of strings representing timestamps in a consistent format.
Returns:
A list of strings representing the sorted timestamps.
"""
# Convert strings to datetime objects for sorting
datetime_objects = [datetime.fromisoformat(record) for record in records]
# Sort the datetime objects
datetime_objects.sort()
# Convert back to strings
sorted_records = [record.strftime('%Y-%m-%d %H:%M:%S') for record, datetime in zip(records, datetime_objects)]
return sorted_records
if __name__ == '__main__':
# Example usage
timestamp_records = [
"2024-01-26 10:00:00",
"2024-01-25 12:30:00",
"2024-01-27 08:45:00",
"2024-01-26 15:15:00"
]
sorted_timestamps = sort_timestamps(timestamp_records)
print(sorted_timestamps)
Add your comment