$ npm install json-function
import { orderBy } from 'json-function';
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"
}
];
orderBy(data, "id", "DESC");
[
{
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"
}
];
orderBy(data, "title", "ASC");
// or
orderBy(data, "title");
[
{
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"
}
];
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 });