OrderBy

With the "orderBy" function you can reorder the data in your json array.

Github repository for your stars.

Usage

Install

$ npm install json-function

Import

import { orderBy } from 'json-function';

Usage

Example Data

const data = [
  {
    id: 1,
    userId: 1,
    title: "quis ut nam facilis et officia qui"
  },
  {
    id: 2,
    userId: 1,
    title: "lorem ipsum"
  },
  {
    id: 3,
    userId: 2,
    title: "delectus aut autem"
  }
];

DESC Syntax

orderBy(data, "id", "DESC");

Output

[
  {
    id: 3,
    userId: 2,
    title: "delectus aut autem"
  },
  {
    id: 2,
    userId: 1,
    title: "lorem ipsum"
  },
  {
    id: 1,
    userId: 1,
    title: "quis ut nam facilis et officia qui"
  }
];

ASC Syntax

orderBy(data, "title", "ASC");
// or
orderBy(data, "title");

Output

[
  {
    id: 3,
    userId: 2,
    title: "delectus aut autem"
  },
  {
    id: 2,
    userId: 1,
    title: "lorem ipsum"
  },
  {
    id: 1,
    userId: 1,
    title: "quis ut nam facilis et officia qui"
  }
];

It is currently only sorting with string and number values. Soon the date ranking support will come.

Sort Deep

Example Data

const data = [
  {
    id: 1,
    userId: 1,
    address: {
      city: "New York"
    }
  },
  {
    id: 2,
    userId: 1,
    address: {
      city: "Detroit"
    }
  },
  {
    id: 3,
    userId: 2,
    address: {
      city: "Dallas"
    }
  }
];
orderBy(data, "address.city", "ASC", { deep: true });

Last updated