JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1508
  • Score
    100M100P100Q109465F
  • License ISC

Sorts an array with multiple ordering criteria

Package Exports

  • array-sort-by

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (array-sort-by) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Array sortBy

The motivation for creating this utility was to provide a mechanism to organize elements, with the ability to specify multiple ordering criteria.

Getting started

To include this library into your package manager with npm or yarn

# with npm
$ npm install array-sort-by --save

# with yarn
$ yarn add array-sort-by

The library has been written as an ES2015 Module and the exported module has the following signature:

/*
 * @param  {Array} array: the list of elements to sort
 * @param  {Function} parser: transforms each item and specifies the sort order
 * @return {Array}
 */
function sortBy(array, /*optional*/ parser);

The optional parameter parser is a function that transforms each element being iterated and sets the sort rules: ascending, descending, and multiple fields for sorting. It has the following signature:

/*
 * @param  {Any} item: the element being iterated over the list
 * @param  {Number} index: the index of the element in the list
 * @return {Any}
 */
function parser(item, index);

Including the library

array-sort-by can be included directly from a CDN in your page:

<script src="https://cdn.rawgit.com/jherax/array-sort-by/1.0.3/dist/sort-by.min.js"></script>

In the above case, the function sortBy is included as a global object in the browser.

As this library is built as an UMD (Universal Module Definition), it can be included from a module loader as AMD, CommonJS, or ES2015 Export.

CommonJS

var sortBy = require('array-sort-by');

ES2015 Export

import sortBy from 'array-sort-by';

AMD

// using RequireJS
requirejs.config({
  paths: {
    // remove the extension .js
    'array-sort-by': '<PATH>/sort-by.min'
  }
});
require(['array-sort-by'], function(sortBy) {
  sortBy(someArray);
});

See an example with RequireJS here: http://jsfiddle.net/FdKTn/66/

Examples

Default sorting (ASC)

let arr = [10, 8, 5, 3, 0, 7, 4, 5, 1];
sortBy(arr);

/*
 * expected:
 * [0, 1, 3, 4, 5, 5, 7, 8, 10]
 */

Sorting DESC

let arr = [5, 1, 8, 0, 3, 7, 10, 4, 3, 8];
sortBy(arr, n => -n);

/*
 * expected:
 * [10, 8, 8, 7, 5, 4, 3, 3, 1, 0]
 */

Sorting DESC date-strings as Date

let arr = ["1983/03/06", "1980/12/24", "1985/08/31", "1983/03/05"];
sortBy(arr, (s) => -new Date(s));

/*
 * expected:
 * ["1985/08/31", "1983/03/06", "1983/03/05", "1980/12/24"]
 */

Sorting DESC strings

Because we use the minus (-) symbol to specify a descending order, it will produce a NaN value when is used with a String element. That's why the flag "desc:" is prefixed to the string items in the parser callback.

let arr = ["1983/03/06", "1980/12/24", "1985/08/31", "1983/03/05"];
sortBy(arr, (s) => "desc:" + s);

/*
 * expected:
 * ["1985/08/31", "1983/03/06", "1983/03/05", "1980/12/24"]
 */

Sorting DESC by @n, after ASC by @d (as Date)

let arr = [
  { a: 8, d: "1985/08/31" },
  { a: 2, d: "1980/12/24" },
  { a: 5, d: "1983/03/06" },
  { a: 8, d: "1983/03/06" }
];

sortBy(arr, (o) => [-o.a, new Date(o.d)]);

/*
 * expected:
 * [
 *   { a: 8, d: "1983/03/06" },
 *   { a: 8, d: "1985/08/31" },
 *   { a: 5, d: "1983/03/06" },
 *   { a: 2, d: "1980/12/24" }
 * ]
 */

Sorting DESC by @name (ignore case sensitive)

let arr = [
  { id: 4, name: "Pedro" },
  { id: 6, name: "Lucia" },
  { id: 7, name: "paco" },
  { id: 3, name: "luis" }
];

sortBy(arr, (o) => "DESC:" + o.name.toUpperCase());

/*
 * expected:
 * [
 *   { id: 4, name: "Pedro" },
 *   { id: 7, name: "paco" },
 *   { id: 3, name: "luis" },
 *   { id: 6, name: "Lucia" }
 * ]
 */

Sorting ASC by @name (ignore case sensitive), after DESC by @age, after ASC by @id

let arr = [
  { a: 4, age: 26, name: "pedro" },
  { a: 6, age: 32, name: "Pedro" },
  { a: 7, age: 26, name: "Luis" },
  { a: 2, age: 26, name: "luis" }
];

sortBy(arr, (o) => [o.name.toUpperCase(), -o.age, o.a]);

/*
 * expected:
 * [
 *   { a: 2, age: 26, name: "luis" },
 *   { a: 7, age: 26, name: "Luis" },
 *   { a: 6, age: 32, name: "Pedro" },
 *   { a: 4, age: 26, name: "pedro" }
 * ]
 */

Running the project

If you want to fork or build your own, you must run this project.

Requirements

  1. Git (git-linux or git-windows).
  2. Node.js (latest stable version v6+).
    It is preferable install nvm (Node Version Manager).
  3. Yarn installed as a global package.

NOTE: Consider install Node Version Manager (nvm) to upgrade easily the NodeJS version.
Go to https://github.com/creationix/nvm and check the installation process for your OS.

If you are running Windows, you can install nvm-windows. Follow every step mentioned here so that nvm will be correctly installed to manage multiple installations of node.js (with npm) on a Windows computer.

Building the project

Clone the repository:

$ git https://github.com/jherax/array-sort-by.git

If you don't have installed yarn as a global package, run this command:

$ npm install -g yarn

Now yarn will install dependencies in package.json:

$ yarn

And finally execute the webpack task:

$ yarn run build

This command will lint the code with ESLint and transpile the source files from src/ to dist/ as an UMD with Babel. It also generates the minified and source map files.

Versioning

This projects adopts the Semantic Versioning (SemVer) guidelines:

<MAJOR>.<MINOR>.<PATCH>

Given a version number MAJOR.MINOR.PATCH, increment the:

  1. MAJOR version when you make incompatible API changes
  2. MINOR version when you add functionality in a backwards-compatible manner
  3. PATCH version when you make backwards-compatible bug fixes.

Issues

To report an issue and keep traceability of bug-fixes, please report to:

License

This project has been released under the ISC license. This license applies ONLY to the source of this repository and does not extend to any other distribution, or any other 3rd party libraries used in a repository. See LICENSE file for more information.