Why might a booking app put a February train before a January train?
You're looking at two Irish Rail bookings. One is for the twenty-eighth of January and the other is for the third of February. You sort the dates, and February comes first. How could the computer get that order?
Start with the January booking. As a string, it's a sequence of characters with slashes between three parts. If the format is day, month, year, you can read those parts as day twenty-eight, month one, and year twenty twenty-six.
In an ascending string sort, the third of February comes before the twenty-eighth of January. Compare the first characters: zero comes before two. Now look at chronological order. Both dates are in the same year, so January comes before February.
Try two more bookings: the seventeenth of January and the ninth of February, both in twenty twenty-six. They're still stored as day-first strings. Which string comes first in an ascending string sort? Pick one answer.
Look at zero three slash zero four slash twenty twenty-six. Day first gives you the third of April. Month first gives you the fourth of March. Don't guess which one was intended. Parsing means using a specified format to turn the text into a date value.
Use day, month, year for both bookings. Parse the first string as the twenty-eighth of January twenty twenty-six. Parse the second as the third of February in the same year. Compare the resulting date values. The years match, and January comes before February.
You have zero six slash zero five slash twenty twenty-six and twenty-two slash zero four slash twenty twenty-six. Use day, month, year for both. Which date value comes first after parsing? Pick one answer.
Import datetime and name its parsing function. Set the format: percent d for day, percent m for month and percent capital Y for the four-digit year. Parse both strings and keep their dates. Store February before January in a list. Sort and print the date values. January comes first.
An exam question might ask, 'Explain why sorting these strings may give an incorrect chronological order. Use an example to support your answer.' Show a pair that sorts incorrectly as text, state why it does, and give the correct order after parsing.