1.0.6 • Published 9 months ago

object_key_sort v1.0.6

Weekly downloads
-
License
ISC
Repository
github
Last release
9 months ago

If array of objects are to be sorted by any key then just provide the name of the key along with the objects it will sort the whole object collection

This is the example:

const sortByKey = require('object_key_sort');

const arrayOfObjects = [
  { name: "Alice", age: 30 },
  { name: "Bob", age: 25 },
  { name: "Charlie", age: 35 },
  // Add more objects here
];

const sortedArray = sortByKey({ data: arrayOfObjects, key: "age", order:'asc' });
console.log(sortedArray);

[
  { name: 'Bob', age: 25 },
  { name: 'Alice', age: 30 },
  { name: 'Charlie', age: 35 }
]

Another Example

const arrayOfObjects = [
  { name: {first:{last:"Xavier"}}, age: 30 },
  { name: {first:{last:"Bob"}}, age: 25 },
  { name: {first:{last:"Charlie"}}, age: 35 },
  // Add more objects here
];

const sortedArray = sortByKey({data: arrayOfObjects, key: "name.first.last", order: 'desc'});
console.log(JSON.stringify(sortedArray));

Output : [
    {"name":{"first":{"last":"Xavier"}},"age":30},
    {"name":{"first":{"last":"Charlie"}},"age":35},
    {"name":{"first":{"last":"Bob"}},"age":25}
  ]
1.0.6

9 months ago

1.0.5

9 months ago

1.0.4

9 months ago

1.0.3

9 months ago

1.0.2

9 months ago

1.0.1

9 months ago

1.0.0

9 months ago