Json Function
Search…
OrderBy
With the "orderBy" function you can reorder the data in your json array.
Github repository for your stars.

Usage

Install

1
$ npm install json-function
Copied!

Import

1
import { orderBy } from 'json-function';
Copied!

Usage

Example Data
1
const data = [
2
{
3
id: 1,
4
userId: 1,
5
title: "quis ut nam facilis et officia qui"
6
},
7
{
8
id: 2,
9
userId: 1,
10
title: "lorem ipsum"
11
},
12
{
13
id: 3,
14
userId: 2,
15
title: "delectus aut autem"
16
}
17
];
Copied!
DESC Syntax
1
orderBy(data, "id", "DESC");
Copied!
Output
1
[
2
{
3
id: 3,
4
userId: 2,
5
title: "delectus aut autem"
6
},
7
{
8
id: 2,
9
userId: 1,
10
title: "lorem ipsum"
11
},
12
{
13
id: 1,
14
userId: 1,
15
title: "quis ut nam facilis et officia qui"
16
}
17
];
Copied!
ASC Syntax
1
orderBy(data, "title", "ASC");
2
// or
3
orderBy(data, "title");
Copied!
Output
1
[
2
{
3
id: 3,
4
userId: 2,
5
title: "delectus aut autem"
6
},
7
{
8
id: 2,
9
userId: 1,
10
title: "lorem ipsum"
11
},
12
{
13
id: 1,
14
userId: 1,
15
title: "quis ut nam facilis et officia qui"
16
}
17
];
Copied!
It is currently only sorting with string and number values. Soon the date ranking support will come.
Sort Deep
Example Data
1
const data = [
2
{
3
id: 1,
4
userId: 1,
5
address: {
6
city: "New York"
7
}
8
},
9
{
10
id: 2,
11
userId: 1,
12
address: {
13
city: "Detroit"
14
}
15
},
16
{
17
id: 3,
18
userId: 2,
19
address: {
20
city: "Dallas"
21
}
22
}
23
];
Copied!
1
orderBy(data, "address.city", "ASC", { deep: true });
Copied!
Last modified 1yr ago
Copy link
Contents
Usage