js-temporal / proposal-temporal-v2

Future additions to Temporal
MIT License
24 stars 1 forks source link

Sortability of Temporal.PlainMonthDay #18

Open ptomato opened 3 years ago

ptomato commented 3 years ago

Should there be a Temporal.PlainMonthDay.compare() static method? Follow up from https://github.com/tc39/proposal-temporal/issues/523.

Advantages:

The main use case is sorting a list of birthdays or anniversaries, holidays that fall on the same date each year, etc. This is easy enough when using the ISO calendar, and human calendars based on it. For example, you can do it in several ways if your instances are guaranteed to have the ISO calendar:

// Rely on the default behaviour of converting to a string and sorting lexicographically:
birthdays.sort();

// Sort by monthCode, then day
birthdays.sort((a, b) => {
  if (a.monthCode === b.monthCode) return a.day - b.day;
  return a.monthCode < b.monthCode ? -1 : 1;
});

// Sort by projecting into Temporal.PlainDate in a leap year
birthdays.sort((a, b) => {
  const year = 2000;
  return Temporal.PlainDate.compare(a.toPlainDate({ year }), b.toPlainDate({ year }));
});

However, if sorting in a calendar with leap months, this is not easy at all and requires making choices about how intercalary months are ordered.

Concerns:

Prior art:

Constraints / corner cases: