An open API service indexing awesome lists of open source software.

https://github.com/tiaanduplessis/prop-sort

Sort array of objects based on property (supports nested properties)
https://github.com/tiaanduplessis/prop-sort

array nested object property sorting

Last synced: 6 months ago
JSON representation

Sort array of objects based on property (supports nested properties)

Awesome Lists containing this project

README

          

prop-sort



Sort array of objects based on property (supports nested properties)





Package version


Downloads


Standard


Travis Build


Standard Readme


GitHub version


Dependency CI


License


PRs


Donate






Github Watch Badge


Github Star Badge


Tweet





Built with ❤︎ by Tiaan and contributors

Table of Contents

Table of Contents

  • Install

  • Usage

  • Contribute

  • License
  • ## Install

    ```sh
    $ npm install --save prop-sort
    # OR
    $ yarn add prop-sort
    ```

    ## Usage

    The original array **is not mutated**.

    ```js
    const propSort = require('prop-sort')

    const data = [
    {
    value: 1,
    priority: 'c'
    },
    {
    value: 2,
    priority: 'a'
    },
    {
    value: 9
    },
    {
    value: 1,
    priority: 'z'
    },
    {
    value: 2,
    priority: 'a'
    }
    ]

    console.log(propSort(data, 'priority'))
    //[ { value: 9 },
    // { value: 2, priority: 'a' },
    // { value: 2, priority: 'a' },
    // { value: 1, priority: 'c' },
    // { value: 1, priority: 'z' } ]

    ```

    Use `dot notation` to sort by nested properties:

    ```js
    const propSort = require('prop-sort')

    const data = [
    {
    value: 1,
    foo: {
    bar: 8
    }
    },
    {
    value: 2,
    foo: {
    bar: 1
    }
    },
    {
    value: 9,
    foo: {
    bar: 5
    }
    },
    {
    value: 1,
    foo: {
    bar: 99
    }
    },
    {
    value: 2,
    foo: {
    bar: 70
    }
    }
    ]

    console.log(propSort(data, 'foo.bar'))
    // [ { value: 2, foo: { bar: 1 } },
    // { value: 9, foo: { bar: 5 } },
    // { value: 1, foo: { bar: 8 } },
    // { value: 2, foo: { bar: 70 } },
    // { value: 1, foo: { bar: 99 } } ]
    ```

    Can also use own custom comparator function:

    ```js
    const propSort = require('prop-sort')

    data = [...]

    propSort(data, 'property', (valueOfFirstProperty, valueOfSecondProperty) => {
    //...
    })

    ```

    ## Contribute

    Contributions are welcome. Please open up an issue or create PR if you would like to help out.

    Note: If editing the README, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification.

    ## License

    Licensed under the MIT License.