sort-objects
v1.1.10
Published
Sort array of objects based on priority provided
Downloads
5
Maintainers
Readme
Sort Objects
sort-objects is a js library for sorting array of objects based on priority.
Installation
Use the package manager [npm] to install sort-objects.
npm i sort-objects
Usage
let ps = require('sort-objects')
let objects = [
{
aaa:"AAAAA",
bbb:1,
ccc:{
ddd:"ddd"
}
},
{
aaa:"ZZZ",
bbb:2,
ccc:{
ddd:"eee"
}
},
{
aaa:"AAAAA",
bbb:2,
ccc:{
ddd:"ddd"
}
}
]
Ascending
The Default is always ascending order
objects.sort(ps.prioritySort([
{
key: "numericString",
cast: (e) => {
return parseInt(e.replace(/^\D+/g, ""));
},
},
"aaa",
"bbb",
"ccc.ddd",
]);
console.log(objects)
Sorted
[
{ aaa: 'AAAAA', bbb: 1, ccc: { ddd: 'ddd' }, numericString: '1AA' },
{ aaa: 'ZZZ', bbb: 2, ccc: { ddd: 'eee' }, numericString: '1AA' },
{ aaa: 'AAAAA', bbb: 2, ccc: { ddd: 'ddd' }, numericString: '3DA' }
]
Descending
objects.sort(ps.prioritySort(['aaa','bbb','ccc.ddd'],"desc"))
console.log(objects)
Sorted
[
{ aaa: 'AAAAA', bbb: 2, ccc: { ddd: 'ddd' }, numericString: '3DA' },
{ aaa: 'ZZZ', bbb: 2, ccc: { ddd: 'eee' }, numericString: '1AA' },
{ aaa: 'AAAAA', bbb: 1, ccc: { ddd: 'ddd' }, numericString: '1AA' }
]