Dev Helpers
← Back

Reverse the chronology of one document

Reverse the chronology of one document. Inspect the transformed data in the console.

Open this example in the interactive library

Source code

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);
console.log(JSON.stringify(result, null, 2));

Sample output

[
  {
    "doc": "guide",
    "rev": 4,
    "author": "Ada"
  },
  {
    "doc": "guide",
    "rev": 3,
    "author": "Kim"
  },
  {
    "doc": "guide",
    "rev": 1,
    "author": "Ada"
  }
]