Skip to content

Extracting a sorted result from a given JSON object and stringify with JSON3.

License

Notifications You must be signed in to change notification settings

Raiyee/json-sorter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json-sorter

Extracting a sorted result from a given JSON object or array even string, parse and stringify with JSON3.

Usage

'use srtrict';

const jsonSorter = require('json-sorter');

jsonSorter(json [,options]);

The first parameter json could be an object or array, even a json string.

By default, options.parse is true, you can set it to false so that it will not try to parse string to object or array. Of course you can pass an user-defined parse function which will trigger a json-string parameter.

options.stringify is used to judge whether to stringify the sorted result, and usually you don't need to set it.

If you don't set options.compare, the json with be sorted according to the dictionary. It with be used to sort the keys of the parsed object like Array.prototype.sort.

See Array.prototype.sort for more information.


A complex example is as follows:

const jsonSorter = require('json-sorter');
const _ = require('underscore');

jsonSorter([{key: 1}, {key: 3}, {key: 2}, {key: {c: 1, d: 2, a: 3, b: 4}}], {
    compare: function (x, y) {
        if (_.isObject(x)) {
            return x.key < y.key;
        }

        return x < y;
    }
}));

// outputs : '[{"key":3},{"key":2},{"key":1},{"key":{"d":2,"c":1,"b":4,"a":3}}]'

Yeah, as yo see it's very simple.

License

MIT License (Expat)

About

Extracting a sorted result from a given JSON object and stringify with JSON3.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published