All Projects β†’ jonschlinkert β†’ Array Sort

jonschlinkert / Array Sort

Licence: mit
Fast and powerful array sorting. Sort an array of objects by one or more properties. Any number of nested properties or custom comparison functions may be used.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Array Sort

Faltu
Search sort, filter, limit an array of objects in Mongo-style.
Stars: ✭ 112 (+93.1%)
Mutual labels:  sort, array
arr-sort
πŸŽ‰ ε€šι‡ζ‘δ»ΆδΈ‹ηš„ζ•°η»„ζŽ’εΊζ–Ήζ³•
Stars: ✭ 20 (-65.52%)
Mutual labels:  array, sort
Data-Structure-Algorithm-Programs
This Repo consists of Data structures and Algorithms
Stars: ✭ 464 (+700%)
Mutual labels:  array, sort
array-sort-by
Powerful mechanism to sort arrays or array of objects by one or more properties. You can also specify a custom comparer function.
Stars: ✭ 37 (-36.21%)
Mutual labels:  array, sort
js-deep-sort-object
Simple module to sort objects recursively by its keys
Stars: ✭ 19 (-67.24%)
Mutual labels:  array, sort
Leetcode
Provide all my solutions and explanations in Chinese for all the Leetcode coding problems.
Stars: ✭ 5,619 (+9587.93%)
Mutual labels:  sort, array
Kakajson
Fast conversion between JSON and model in Swift.
Stars: ✭ 867 (+1394.83%)
Mutual labels:  array
Pgo
Go library for PHP community with convenient functions
Stars: ✭ 51 (-12.07%)
Mutual labels:  array
Shallow Clone
Make a shallow clone of an object, array or primitive.
Stars: ✭ 23 (-60.34%)
Mutual labels:  array
Eloquent Sortable
Sortable behaviour for Eloquent models
Stars: ✭ 914 (+1475.86%)
Mutual labels:  sort
React Grid Table
A modular table, based on a CSS grid layout, optimized for customization.
Stars: ✭ 57 (-1.72%)
Mutual labels:  sort
Format Graphql
Formats GraphQL schema definition language (SDL) document.
Stars: ✭ 55 (-5.17%)
Mutual labels:  sort
Sma
Calculate the simple moving average of an array.
Stars: ✭ 48 (-17.24%)
Mutual labels:  array
Cracking The Coding Interview
Solutions for Cracking the Coding Interview - 6th Edition
Stars: ✭ 35 (-39.66%)
Mutual labels:  array
Golang Combinations
Golang library which provide an algorithm to generate all combinations out of a given string array.
Stars: ✭ 51 (-12.07%)
Mutual labels:  array
Sortpem
➿ Sorting utility for PEM files
Stars: ✭ 11 (-81.03%)
Mutual labels:  sort
Zarr.js
Javascript implementation of Zarr
Stars: ✭ 54 (-6.9%)
Mutual labels:  array
Query
Query adds tools to aid the use of Ecto in web settings.
Stars: ✭ 23 (-60.34%)
Mutual labels:  sort
Vue Slicksort
A set of vue mixins to turn any list into an animated, touch-friendly, sortable list ✌️
Stars: ✭ 1,010 (+1641.38%)
Mutual labels:  sort
Geeksforgeeks Dsa 2
This repository contains all the assignments and practice questions solved during the Data Structures and Algorithms course in C++ taught by the Geeks For Geeks team.
Stars: ✭ 53 (-8.62%)
Mutual labels:  array

array-sort NPM version NPM monthly downloads NPM total downloads Linux Build Status Windows Build Status

Fast and powerful array sorting. Sort an array of objects by one or more properties. Any number of nested properties or custom comparison functions may be used.

Install

Install with npm:

$ npm install --save array-sort

Install with yarn:

$ yarn add array-sort

Usage

Sort an array by the given object property:

var arraySort = require('array-sort');

arraySort([{foo: 'y'}, {foo: 'z'}, {foo: 'x'}], 'foo');
//=> [{foo: 'x'}, {foo: 'y'}, {foo: 'z'}]

Reverse order

arraySort([{foo: 'y'}, {foo: 'z'}, {foo: 'x'}], 'foo', {reverse: true});
//=> [{foo: 'z'}, {foo: 'y'}, {foo: 'x'}]

Params

arraySort(array, comparisonArgs);
  • array: {Array} The array to sort
  • comparisonArgs: {Function|String|Array}: One or more functions or object paths to use for sorting.

Examples

Sort blog posts

var arraySort = require('array-sort');

var posts = [
  { path: 'c.md', locals: { date: '2014-01-09' } },
  { path: 'a.md', locals: { date: '2014-01-02' } },
  { path: 'b.md', locals: { date: '2013-05-06' } },
];

// sort by `locals.date`
console.log(arraySort(posts, 'locals.date'));

// sort by `path`
console.log(arraySort(posts, 'path'));

Sort by multiple properties

var arraySort = require('array-sort');

var posts = [
  { locals: { foo: 'bbb', date: '2013-05-06' }},
  { locals: { foo: 'aaa', date: '2012-01-02' }},
  { locals: { foo: 'ccc', date: '2014-01-02' }},
  { locals: { foo: 'ccc', date: '2015-01-02' }},
  { locals: { foo: 'bbb', date: '2014-06-01' }},
  { locals: { foo: 'aaa', date: '2014-02-02' }},
];

// sort by `locals.foo`, then `locals.date`
var result = arraySort(posts, ['locals.foo', 'locals.date']);

console.log(result);
// [ { locals: { foo: 'aaa', date: '2012-01-02' } },
//   { locals: { foo: 'aaa', date: '2014-02-02' } },
//   { locals: { foo: 'bbb', date: '2013-05-06' } },
//   { locals: { foo: 'bbb', date: '2014-06-01' } },
//   { locals: { foo: 'ccc', date: '2014-01-02' } },
//   { locals: { foo: 'ccc', date: '2015-01-02' } } ]

Custom function

If custom functions are supplied, array elements are sorted according to the return value of the compare function. See the docs for Array.sort() for more details.

var arr = [
  {one: 'w', two: 'b'},
  {one: 'z', two: 'a'},
  {one: 'x', two: 'c'},
  {one: 'y', two: 'd'},
];

function compare(prop) {
  return function (a, b) {
    return a[prop].localeCompare(b[prop]);
  };
}

var result = arraySort(arr, function (a, b) {
  return a.two.localeCompare(b.two);
});

console.log(result);
// [ { one: 'z', two: 'a' },
//   { one: 'w', two: 'b' },
//   { one: 'x', two: 'c' },
//   { one: 'y', two: 'd' } ]

Multiple custom functions

var arr = [
  {foo: 'w', bar: 'y', baz: 'w'},
  {foo: 'x', bar: 'y', baz: 'w'},
  {foo: 'x', bar: 'y', baz: 'z'},
  {foo: 'x', bar: 'x', baz: 'w'},
];

// reusable compare function
function compare(prop) {
  return function (a, b) {
    return a[prop].localeCompare(b[prop]);
  };
}

// the `compare` functions can be a list or array
var result = arraySort(arr, compare('foo'), compare('bar'), compare('baz'));

console.log(result);
// [ { foo: 'w', bar: 'y', baz: 'w' },
//   { foo: 'x', bar: 'x', baz: 'w' },
//   { foo: 'x', bar: 'y', baz: 'w' },
//   { foo: 'x', bar: 'y', baz: 'z' } ]

About

Related projects

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Contributors

Commits Contributor
10 jonschlinkert
4 doowb
1 iamstolis
1 wkevina

Building docs

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

Running tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test

Author

Jon Schlinkert

License

Copyright Β© 2017, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.6.0, on September 11, 2017.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].