just-order-by
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

just-order-by

Part of a library of zero-dependency npm modules that do just do one thing. Guilt-free utilities for every occasion.

🍦 Try it

npm install just-order-by
yarn add just-order-by

Produces a new array, sorted in given order

import orderBy from 'just-order-by';

orderBy([10, 1, 5, 20, 15, 35, 30, 6, 8]); // [1, 5, 6, 8, 10, 15, 20, 30, 35]

orderBy(
  [
    { user: 'fabio', details: { city: 'Milan', age: 34 } },
    { user: 'max', details: { city: 'Munich', age: 29 } },
    { user: 'zacarias', details: { city: 'Sao Paulo', age: 44 } },
    { user: 'robert', details: { city: 'Manchester', age: 28 } },
    { user: 'max', details: { city: 'Zurich', age: 38 } },
  ],
  [
    {
      property(v) {
        return v.details.age;
      },
    },
  ]
);

/*
[
  {user: 'robert', age: 28},
  {user: 'max', age: 29},
  {user: 'fabio', age: 34},
  {user: 'klaus', age: 38},
  {user: 'zacarias', age: 44},
]
*/

orderBy(
  [
    {user: 'fabio', age: 34},
    {user: 'max', age: 29},
    {user: 'zacarias', age: 44},
    {user: 'robert', age: 28},
    {user: 'klaus', age: 38},
  ],
  [
    {
      property: 'user',
    },
  ]
);

/*
[
  {user: 'fabio', age: 34},
  {user: 'klaus', age: 38},
  {user: 'max', age: 29},
  {user: 'robert', age: 28},
  {user: 'zacarias', age: 44},
]
*/

orderBy(
  [
    { user: 'fabio', age: 34 },
    { user: 'max', age: 29 },
    { user: 'zacarias', age: 44 },
    { user: 'moris', age: 28 },
    { user: 'max', age: 38 },
  ],
  [
    {
      property: 'user',
      order: 'desc',
    },
    {
      property(v) {
        return v.age;
      },
    },
  ]
);

/*
[
  {
    user: 'zacarias',
    age: 44
  },
  {
    user: 'moris',
    age: 28
  },
  {
    user: 'max',
    age: 29
  },
  {
    user: 'max',
    age: 38
  },
  {
    user: 'fabio',
    age: 34
  }
]
*/

Readme

Keywords

Package Sidebar

Install

npm i just-order-by

Weekly Downloads

7,828

Version

1.0.0

License

MIT

Unpacked Size

8.13 kB

Total Files

8

Last publish

Collaborators

  • angus-c
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy