Coder Social home page Coder Social logo

sort-object-properties's Introduction

sort-object-properties

Utility that return copy of object with sorted keys

Installation

npm install sort-object-properties --save

Usage

Import

//ES6
import sort from 'sort-object-properties';
//CommonJS
var sort = require('sort-object-properties');

Simple call

const objectWithUnsortedKeys = {
    d: 'value1',
    c: 'value2',
    e: 'value3',
    b: 'value4',
    a: 'value5'
};

var sortedObject = sort(objectToSort);

console.log(sortedObject); 
/*
    a: 'value5',
    b: 'value4',
    c: 'value2',
    d: 'value1',
    e: 'value3'
*/

API

Global function

By default sorts object ascending by its keys. Use second parameter if need more flexibility.

Argument Type Optional Description
obj Object false Object to sort
sortFunction SortFunction true Function used to sort object

SortFunction

Argument Type Optional Description
a PropertyObject true Left side property
b PropertyObject true Right side property

PropertyObject

Property Type Optional Description
key string, number, Symbol true Key used in parent object
value any true Value assigned to key

value function

Sorts object properties by its values.

Argument Type Optional Description
obj Object false Object to sort
direction SortDirection, Number true Sort direction
valueSelector Function true Function that return simple value from complex type properties

key function

Sorts object properties by its keys.

Argument Type Optional Description
obj PropertyObject false Object to sort
direction SortDirection, Number true Sort direction

sortDirection constant

Constant with sort directions, use that const or just use plain numbers in function calls.

Name Value
ascending 1
descending -1

Examples

Sort by values using global function

const objectWithUnsortedValues = {
    key1: 'd',
    key2: 'a',
    key3: 'e',
    key4: 'b',
    key5: 'c'
};

//ES6 syntax
var sortedObject = sort(objectWithUnsortedValues, ({value: valueA}, {value: valueB}) => valueA > valueB);
//ES5 syntax
var sortedObject = sort(objectWithUnsortedValues, function (a, b){ return a.value > b.value;});

console.log(sortedObject);
/*
    key2: 'a',
    key4: 'b',
    key5: 'c'
    key1: 'd',
    key3: 'e',
*/

Sort by values using values function

import { value as sortByValue } from 'sort-object-properties';

const objectWithUnsortedValues = {
    key1: 'd',
    key2: 'a',
    key3: 'e',
    key4: 'b',
    key5: 'c'
};

var sortedObject = sortByValue(objectWithUnsortedValues);

console.log(sortedObject);
/*
    key2: 'a',
    key4: 'b',
    key5: 'c'
    key1: 'd',
    key3: 'e',
*/

Sort by keys using key function

import { key as sortByKey, sortDirection } from 'sort-object-properties';

const objectWithUnsortedKeys = {
    key4: 'e',
    key1: 'c',
    key3: 'c',
    key2: 'd',
    key5: 'a'
};

var sortedObject = sortByKey(objectWithUnsortedKeys, sortDirection.descending);

console.log(sortedObject);
/*
    key5: 'a',
    key4: 'e',
    key3: 'c',
    key2: 'd',
    key1: 'c'
*/

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.