JSPM

json-array-sorter

0.0.4
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q40081F
  • License MIT

Sort an array of objects (or a similar json file) by one or more properties in ascending or descending order. Using 'require' or the CLI.

Package Exports

  • json-array-sorter

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (json-array-sorter) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

json-array-sorter

Sort an array of objects (or a similar json file) by one or more properties in ascending or descending order. Using 'require' or the CLI.

What is this?

This is an JS array of objects sorter. Say you have the following data:

[
    {
        "id": "Book 1",
        "title": "Java How To Program",
        "author": "Deitel & Deitel",
        "year": 2007
    },
    {
        "id": "Book 2",
        "title": "Patterns of Enterprise Application Architecture",
        "author": "Martin Fowler",
        "year": 2002
    },
    {
        "id": "Book 3",
        "title": "Head First Design Patterns",
        "author": "Elisabeth Freeman",
        "year": 2004
    },
    {
        "id": "Book 4",
        "title": "Internet & World Wide Web: How To Program",
        "author": "Deitel & Deitel",
        "year": 2007
    }
]

And you use the following configuration object:

[
    {
        "column": "year",
        "order": "descending"
    },
    {
        "column": "author",
        "order": "descending"
    },
    {
        "column": "title",
        "order": "ascending"
    }
]

The sorter will return the following object (or generate a .json file, depending on how you are using it):

[
    {
        id: 'Book 4',
        title: 'Internet & World Wide Web: How To Program',
        author: 'Deitel & Deitel',
        year: 2007
    },
    {
        id: 'Book 1',
        title: 'Java How To Program',
        author: 'Deitel & Deitel',
        year: 2007
    },
    {
        id: 'Book 3',
        title: 'Head First Design Patterns',
        author: 'Elisabeth Freeman',
        year: 2004
    },
    {
        id: 'Book 2',
        title: 'Patterns of Enterprise Application Architecture',
        author: 'Martin Fowler',
        year: 2002
    }
]

The test.js show a few other configuration objects but that's most ellaborate one.

How to use it?

You'll need Node.js. Then you can require it:

sorter = require('json-array-sorter')

Give it two arrays (a configuration one and a data one) and get a sorted array back:

let sortedArray = sorter(
    [
        {
            "column": "title",
            "order": "ascending"
        }
    ],
    [
        {
            "id": "Book 1",
            "title": "Java How To Program",
            "author": "Deitel & Deitel",
            "year": 2007
        },
        {
            "id": "Book 2",
            "title": "Patterns of Enterprise Application Architecture",
            "author": "Martin Fowler",
            "year": 2002
        },
        {
            "id": "Book 3",
            "title": "Head First Design Patterns",
            "author": "Elisabeth Freeman",
            "year": 2004
        },
        {
            "id": "Book 4",
            "title": "Internet & World Wide Web: How To Program",
            "author": "Deitel & Deitel",
            "year": 2007
        }
    ]
)

Using the CLI, BUT NOT REALLY

If you have it installed you can go its folder and use directly passing three arguments.

 node sorter config.json data.json output.json

You must pass the location of the configuration .json file, the data .json file and a name for the output file.

I am considering have it being able to be installed globally and so you can use from anywhere... Not sure how to do it best, I am not a great package creator, yet. =P

Is it good to go?

Well... It works. =)

It's just a beta for now, and I'm not sure how useful it is in the real world. But you should try it yourself and perhaps send me a pull request eventually. This my first npm package and was created as an exercise so it is not great, yet.

Do get in touch if you have any questions or a good use case for it. =P