npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-array-sorter

v0.0.5

Published

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.

Downloads

34

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


Technical Assesment Instructions: just download it and run node test or npm test. Or install it using npm and then cd into the json-array-sorter folder inside node_modules and run node test.