Dev Helpers
← Back

Reverse the chronology of one document

Reverse the chronology of one document. Build the result in an App Router server page and send the rendered entries to the client.

Open this example in the interactive library

Source code

// app/reverse-the-chronology-of-one-document/page.tsx
export const metadata = { title: "Reverse the chronology of one document" };
export default function Page() {
  const rows = [
    { doc: "guide", rev: 1, author: "Ada" },
    { doc: "api", rev: 1, author: "Leo" },
    { doc: "guide", rev: 3, author: "Kim" },
    { doc: "guide", rev: 4, author: "Ada" },
  ];
  const result = rows
    .filter((r) => r.doc === "guide")
    .toSorted((a, b) => b.rev - a.rev);
  const entries = Object.entries(
    typeof result === "object" && result !== null ? result : { value: result },
  );
  return (
    <section>
      <h2>{"Reverse the chronology of one document"}</h2>
      {entries.length ? (
        <dl>
          {entries.map(([key, value]) => (
            <div key={key}>
              <dt>{key}</dt>
              <dd>
                {typeof value === "string" ? value : JSON.stringify(value)}
              </dd>
            </div>
          ))}
        </dl>
      ) : (
        <p>No matching entries.</p>
      )}
    </section>
  );
}