All Projects β†’ tywei90 β†’ arr-sort

tywei90 / arr-sort

Licence: MIT license
πŸŽ‰ ε€šι‡ζ‘δ»ΆδΈ‹ηš„ζ•°η»„ζŽ’εΊζ–Ήζ³•

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to arr-sort

js-deep-sort-object
Simple module to sort objects recursively by its keys
Stars: ✭ 19 (-5%)
Mutual labels:  array, sort
Array Sort
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.
Stars: ✭ 58 (+190%)
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 (+85%)
Mutual labels:  array, sort
Faltu
Search sort, filter, limit an array of objects in Mongo-style.
Stars: ✭ 112 (+460%)
Mutual labels:  array, sort
Leetcode
Provide all my solutions and explanations in Chinese for all the Leetcode coding problems.
Stars: ✭ 5,619 (+27995%)
Mutual labels:  array, sort
Data-Structure-Algorithm-Programs
This Repo consists of Data structures and Algorithms
Stars: ✭ 464 (+2220%)
Mutual labels:  array, sort
sublime-postcss-sorting
Sublime Text plugin to sort CSS rules content with specified order.
Stars: ✭ 19 (-5%)
Mutual labels:  sort
batching-toposort
Efficiently sort interdependent tasks into a sequence of concurrently-executable batches
Stars: ✭ 21 (+5%)
Mutual labels:  sort
sorting-visualization
🎨 A command-line tool to generate GIF which can display sorting algorithm
Stars: ✭ 37 (+85%)
Mutual labels:  sort
gulp-sort
Sort files in stream by path or any custom sort comparator
Stars: ✭ 22 (+10%)
Mutual labels:  sort
arrays
Yii Array Helper
Stars: ✭ 41 (+105%)
Mutual labels:  array
vim-sort-imports
vim port of import-sort. Sort javascript/typescript imports
Stars: ✭ 32 (+60%)
Mutual labels:  sort
PixelGlitch
Image glitch visualization using various Pixel Sorting methods for Processing
Stars: ✭ 25 (+25%)
Mutual labels:  sort
slimarray
SlimArray compresses uint32 into several bits, by using a polynomial to describe overall trend of an array.
Stars: ✭ 39 (+95%)
Mutual labels:  array
Ubigeo-Peru
Base de datos de departamentos, provincias y distritos del PerΓΊ (UBIGEO) actualizada al 2019 (El INEI ha actualizado hasta el 2016). SQL, JSON, XML, CSV, Arreglos PHP, YAML.
Stars: ✭ 113 (+465%)
Mutual labels:  array
repository
[PHP 7] Implementation and definition of a base Repository in Domain land.
Stars: ✭ 26 (+30%)
Mutual labels:  sort
Lattice2
FreeCAD workbench about arrays of all sorts and kinds, and local coordinate systems
Stars: ✭ 40 (+100%)
Mutual labels:  array
DS ALGO
Data Structures and algorithms
Stars: ✭ 20 (+0%)
Mutual labels:  array
xml-to-array
A simple class to convert an xml to array
Stars: ✭ 30 (+50%)
Mutual labels:  array
Static-Sort
A simple C++ header-only library for fastest sorting of small arrays. Generates sorting networks on compile time via templates.
Stars: ✭ 30 (+50%)
Mutual labels:  sort

arr-sort GitHub license NPM version NPM monthly downloads NPM total downloads Windows Build Status

δΈ­ζ–‡

Sort an object array by one or more properties even nested properties. Besides, you can determine the direction even supply a comparison function in each property sorting.

Update

1.2.5 Version provide better performance at nearly 2x

1.2.0 Version can provide much better performance at nearly 100x! Via code optimization and algorithm optimization. Such as when the length of input array is 2000, the array sorting time, 1.1.0 Version use probably in 10s, 1.2.0 Version just use in 100ms.

Install

Install with npm:

$ npm install --save arr-sort

Install with yarn:

$ yarn add arr-sort

Usage

Sort an array by the given object property:

var arrSort = require('arr-sort');

var arr = [{foo: 'y'}, {foo: 'z'}, {foo: 'x'}];
arrSort(arr, [{attr:'foo'}]);
//=> [{foo: 'x'}, {foo: 'y'}, {foo: 'z'}]

Reverse order

var arr = [{foo: 'y'}, {foo: 'z'}, {foo: 'x'}];
arrSort(arr, [{attr:'foo', asc: false}]);
//=> [{foo: 'z'}, {foo: 'y'}, {foo: 'x'}]

Params

arrSort(array, comparisonArgs);
  • array: { Object Array } The object array to sort
  • comparisonArgs: { Object Array } One or more objects to sort by. The element structure is like this: { 'attr': attr, 'asc': asc }
    • attr: { String } the attribute of the object
    • asc: { Boolean | Function } point the direaction of sorting.
      • true: sort by ascending direction (default)
      • false: sort by descending direction
      • function: sort by a comparable function

Note

  • If attr is not found in object, this sorting round would be skip.
  • The value of attr can be a string or a number.
    • If string, we use localeCompare to sort by.
    • If number, we just compare the amount of the number.
  • The comparison function must follow the sort function specification, if it is equal, it must return 0, otherwise the subsequent sorting will not participate! If there is not a return value of function, this sorting round would be skip.

Examples

1. Sort by multiple properties

var arrSort = require('arr-sort');

var array = [
  { foo: 'bbb', num: 4,  flag: 2 },
  { foo: 'aaa', num: 3,  flag: 1 },
  { foo: 'ccc', num: -6, flag: 2 },
  { foo: 'ccc', num: 8,  flag: 2 },
  { foo: 'bbb', num: 2,  flag: 4 },
  { foo: 'aaa', num: -3, flag: 4 }
];

// sort by `flag`, then `foo`, then `num`
var result = arrSort(array,
    [{
        attr: 'flag',
        asc: true
    },
    {
        attr: 'foo',
        asc: false
    },
    {
        attr: 'num',
        asc: true
    }]
);

console.log(result);
// [ { foo: 'aaa', num: 3,  flag: 1},
//   { foo: 'ccc', num: -6, flag: 2},
//   { foo: 'ccc', num: 8,  flag: 2},
//   { foo: 'bbb', num: 4,  flag: 2},
//   { foo: 'bbb', num: 2,  flag: 4},
//   { foo: 'aaa', num: -3, flag: 4} ]

2. Sort by nested properties

var arrSort = require('arr-sort');

var array = [
  { locals: { foo: 'bbb', num: 4 },  flag: 2},
  { locals: { foo: 'aaa', num: 3 },  flag: 1},
  { locals: { foo: 'ccc', num: -6 }, flag: 2},
  { locals: { foo: 'ccc', num: 8 },  flag: 2},
  { locals: { foo: 'bbb', num: 2 },  flag: 4},
  { locals: { foo: 'aaa', num: -3 }, flag: 4},
];

// sort by `flag`, then `locals.foo`, then `locals.num`
var result = arrSort(array,
    [{
        attr: 'flag',
        asc: true
    },
    {
        attr: 'locals.foo',
        asc: false
    },
    {
        attr: 'locals.num',
        asc: true
    }]
);

console.log(result);
// [ { locals: { foo: 'aaa', num: 3 },  flag: 1},
//   { locals: { foo: 'ccc', num: -6 }, flag: 2},
//   { locals: { foo: 'ccc', num: 8 },  flag: 2},
//   { locals: { foo: 'bbb', num: 4 },  flag: 2},
//   { locals: { foo: 'bbb', num: 2 },  flag: 4},
//   { locals: { foo: 'aaa', num: -3 }, flag: 4} ]

3. Sort by custom function

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

var arrSort = require('arr-sort');

var array = [
  { locals: { foo: 'bbb', num: 4 },  flag: -2},
  { locals: { foo: 'aaa', num: 3 },  flag: 1},
  { locals: { foo: 'ccc', num: -6 }, flag: 2},
  { locals: { foo: 'ccc', num: 8 },  flag: 2},
  { locals: { foo: 'bbb', num: 2 },  flag: 4},
  { locals: { foo: 'aaa', num: -3 }, flag: 4},
];

// sort by `flag`, then `locals.foo`, then `locals.num`
var result = arrSort(array,
    [{
        attr: 'flag',
        asc: function(a,b){return (Math.abs(a) - Math.abs(b))}
    },
    {
        attr: 'locals.foo',
        asc: false
    },
    {
        attr: 'locals.num',
        asc: true
    }]
);

console.log(result);
// [ { locals: { foo: 'aaa', num: 3 },  flag: 1},
//   { locals: { foo: 'ccc', num: -6 }, flag: 2},
//   { locals: { foo: 'ccc', num: 8 },  flag: 2},
//   { locals: { foo: 'bbb', num: 4 },  flag: -2},
//   { locals: { foo: 'bbb', num: 2 },  flag: 4},
//   { locals: { foo: 'aaa', num: -3 }, flag: 4} ]

About

Related projects

  • arr-del: Delete array elements in one time by array consists of their indexes. | homepage

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

tywei90

License

Copyright Β© 2018, tywei90. Released under the MIT License.

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].