Skip to content
Agents tracked: 440 Downloads (7d): 202M down 19.5% GitHub stars: 10.4M Releases (7d): 607 Agent status: 1 with issuesSince last limit reset: Claude 28d · Codex 0d Updated Oct 21, 2026

Sort objects by a date field in date-fns

Real data is usually a list of records with a date field, not bare dates. This example sorts tasks by their due date.

All date-fns examples date-fns: usage and stats

Sort objects by a date field in date-fns: screenshot of the example below running
The code below, running in Chromium on our server (Oct 14, 2026). Press Run to see it live and change it.

How it works

  1. Store dates on objects. Each task has a name and a due field that holds a native Date. [1]
  2. Compare the date field. The sort callback (a, b) => compareAsc(a.due, b.due) passes the two due dates to compareAsc. The tasks are printed earliest due date first. [1]
  3. Print each task. Each line shows the formatted due date and the task name. format builds the yyyy-MM-dd text. [1]

Run and edit it

It runs in your browser, in a sandboxed frame that cannot see this site; the packages load from esm.sh only after you press Run.

import { compareAsc, format } from "date-fns";

const tasks = [
  { name: "Write report", due: new Date(2024, 5, 14) },
  { name: "Book flights", due: new Date(2024, 4, 2) },
  { name: "Renew passport", due: new Date(2024, 3, 28) },
  { name: "Team review", due: new Date(2024, 5, 3) },
];

const byDue = [...tasks].sort((a, b) => compareAsc(a.due, b.due));

console.log("Tasks by due date:");
for (const t of byDue) {
  console.log(`  ${format(t.due, "yyyy-MM-dd")}  ${t.name}`);
}
Ctrl/⌘ + Enter runs

Free for signed-in members, with a daily limit. Describe the change in plain words; the new code runs only in your browser.

Rename the due field or add tasks to try it with your own data. Code written by AgentGid [1].

Checked Oct 14, 2026: it starts without errors in Chromium with date-fns 4.4.0. Needs WebGL.

More date-fns examples

Format a date as yyyy-MM-dd in date-fns Sort dates oldest first in date-fns Sort dates newest first in date-fns Group dates by day in date-fns

Sources

  1. date-fns README (GitHub)

Explanations written by AgentGid from the official docs; the numbers in brackets point here. The code marked "written by AgentGid" is ours and free to copy.

Building something with date-fns? Ask AgentGid →