Canonical ordering of days of week

It is not much shorter than your PositionIndex[{Monday, Tuesday, Wednesday, Thursday, Friday}], but you can also use System`DateObjectDump`$dowAssociation:

System`DateObjectDump`$dowAssociation

<|Monday -> 1, Tuesday -> 2, Wednesday -> 3, Thursday -> 4, Friday -> 5, Saturday -> 6, Sunday -> 7|>

sortByDoW = SortBy[System`DateObjectDump`$dowAssociation];

Examples:

sortByDoW @ {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}

{Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}

sortByDoW @ RandomSample[{Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}]

{Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}


As you've noticed, the weekday symbols are by default sorted by their symbol names. This is because Monday,... are not true date specifications, but only day types. So there is not really a canonical ordering. However, you could do the following:

SortBy[DayPlus[DayPlus[Today, 1, Sunday], 1, #] &]@
  {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}
(* {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday} *)

This works by converting the day types into actual dates. This is done in three steps:

  • Find the next Sunday after today (you could also hardcode the date of a sunday here, but this is more readable I think)
  • Find the date of the first Monday/... after that point
  • Sort these dates

Tags:

Sorting