# Canonical ordering of days of week

The entities representing the days of the week sort alphabetically:

Sort[{Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}]

(* {Friday, Monday, Saturday, Sunday, Thursday, Tuesday, Wednesday} *)


(Note that these are not strings.)

Is there a better way to sort than using the following?

SortBy[PositionIndex[{Monday, Tuesday, Wednesday, Thursday, Friday}]]

• Wouldn't this be ill-defined because it is cyclic? I guess you could order them if you agree on which day is the first of the week… some say monday, others sunday… Sep 19, 2019 at 11:28
• "Canonically" (i.e. according to the Bible) the first day was Sunday. Sep 19, 2019 at 20:43
• I don't know if you can get more canonical than the Bible, but according to ISO 8601, the week begins on Monday. Sep 20, 2019 at 7:30

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


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

sortByDoW = SortBy[SystemDateObjectDump\$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}

Is there a better way to sort than using the following?

SortBy[PositionIndex[{Monday, Tuesday, Wednesday, Thursday, Friday}]]

No, that is the best method.

Note that the accepted answer for this question references a private variable that disappeared around version 12.1 or 12.2

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

Use ResourceFunction["SortLike"] which sorts a list $$l_1$$ so it respects the order in second argument $$l_2$$

days = {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};

SeedRandom[1];
randomdays = RandomChoice[days, 20]

(* {Sunday, Friday, Wednesday, Friday, Monday, Tuesday, Sunday, Monday,
Monday, Wednesday, Monday, Sunday, Monday, Thursday, Saturday,
Wednesday, Monday, Thursday, Friday, Friday} *)

ResourceFunction["SortLike"][randomdays, days]

(* {Monday, Monday, Monday, Monday, Monday, Monday, Tuesday, Wednesday,
Wednesday, Wednesday, Thursday, Thursday, Friday, Friday, Friday,
Friday, Saturday, Sunday, Sunday, Sunday} *)