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-sorting

v1.0.2

Published

Function used to sort the array having object literals

Downloads

7

Readme

json-sorting

It helps to sort the json file either ascending or descending order using the desired key.

Before using this function first parse the json file and make it as a single array containing the series of objects.Before using this function first parse the json file and make it as a single array containing the series of objects.

Installation

npm install --save json-sorting

Syntax

jsonSorting(array_name, key_path, value_type, sort_order);

Parameters : 
array_name = Parsed json array;
key_path = String datatype - give the key position int that array // name.first
value_type = String data type - give the value type whether it is int or string

Optional parameter :
sort_order = string type - choose sort order ascending or descending by using asc or desc

Note : By default it will sort in ascending order

Usage

const fs = require("fs");
const { jsonSorting } = require("json-sorting");

// first read and parse the json file 
const file = fs.readFileSync("./src/user.json");
const usersArray = JSON.parse(file);

// Now use the sort function

let ascending_order = jsonSorting(usersArray, "name.first", "string", "asc");
let descending_order = jsonSorting(usersArray, "name.first", "string", "desc");

Example 1 :

//install and import the package
const { jsonSorting } = require("json-sorting");

// example parsed json file
var json = [{
    "name": "misty",
    "id": 8
}, {
    "name": "ash",
    "id": 6
}, {
    "id": 1
}];

let result = jsonSorting( json, "name", "string");
console.log(result);

// output
	[{
		"name": "ash",
		"id": 6
	}, {
		"name": "misty",
		"id": 8
	}, {
		"id": 1
	}];

In the above example it is sorted using the key - name since all the name are string datatype we are giving string as third parameter. If we notice we didnt give forth parameter by default it is arranged in ascending order

Note if an object doesnt have tht key all the undefined keys objects will be go atlast in the sortingNote if an object doesnt have tht key all the undefined keys objects will be go atlast in the sorting

**Example 2 : **

const { jsonSorting } = require("json-sorting");

var json = [{
    "name": {
      "first": "anita",
      "last": "turner"
    },
    "email": "[email protected]"
  },
  {
    "name": {
      "first": "oscar",
      "last": "wells"
    },
    "email": "[email protected]"
  },
  {
    "name": {
      "first": "george",
      "last": "young"
    },
    "email": "[email protected]"
  }
 ];

let result = jsonSorting( json, "name.first", "string","desc");
console.log(result);

// output
[{
  {
    "name": {
      "first": "oscar",
      "last": "wells"
    },
    "email": "[email protected]"
  },
  {
    "name": {
      "first": "george",
      "last": "young"
    },
    "email": "[email protected]"
  },
  "name": {
      "first": "anita",
      "last": "turner"
    },
    "email": "[email protected]"
  }
 ];

Author and contributor

Balamurugan J GitHub Linked In

License

MIT