5
$\begingroup$

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}]]
$\endgroup$
3
  • $\begingroup$ 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… $\endgroup$
    – SHuisman
    Sep 19, 2019 at 11:28
  • $\begingroup$ "Canonically" (i.e. according to the Bible) the first day was Sunday. $\endgroup$
    – mikado
    Sep 19, 2019 at 20:43
  • 1
    $\begingroup$ I don't know if you can get more canonical than the Bible, but according to ISO 8601, the week begins on Monday. $\endgroup$ Sep 20, 2019 at 7:30

4 Answers 4

6
$\begingroup$

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}

$\endgroup$
4
$\begingroup$

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

$\endgroup$
3
$\begingroup$

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
$\endgroup$
2
$\begingroup$

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} *)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.