|
| 1 | +# {%= name %} {%= badge("fury") %} |
| 2 | + |
| 3 | +> {%= description %} |
| 4 | +
|
| 5 | +## Install |
| 6 | +{%= include("install-npm", {save: true}) %} |
| 7 | + |
| 8 | +## Usage |
| 9 | + |
| 10 | +Sort an array by the given object property: |
| 11 | + |
| 12 | +```js |
| 13 | +var arraySort = require('{%= name %}'); |
| 14 | + |
| 15 | +arraySort([{foo: 'y'}, {foo: 'z'}, {foo: 'x'}], 'foo'); |
| 16 | +//=> [{foo: 'x'}, {foo: 'y'}, {foo: 'z'}] |
| 17 | +``` |
| 18 | + |
| 19 | +**Reverse order** |
| 20 | + |
| 21 | +```js |
| 22 | +arraySort([{foo: 'y'}, {foo: 'z'}, {foo: 'x'}], 'foo', {reverse: true}); |
| 23 | +//=> [{foo: 'z'}, {foo: 'y'}, {foo: 'x'}] |
| 24 | +``` |
| 25 | + |
| 26 | +## Table of contents |
| 27 | + |
| 28 | +<!-- toc --> |
| 29 | + |
| 30 | +## Params |
| 31 | + |
| 32 | +```js |
| 33 | +arraySort(array, comparisonArgs); |
| 34 | +``` |
| 35 | + |
| 36 | +- `array`: **{Array}** The array to sort |
| 37 | +- `comparisonArgs`: **{Function|String|Array}**: Any number of functions, object paths, or arrays or objects paths and functions may be passed. |
| 38 | + |
| 39 | + |
| 40 | +## Examples |
| 41 | + |
| 42 | +### Object paths |
| 43 | + |
| 44 | +Say you have the following blog posts: |
| 45 | + |
| 46 | +```js |
| 47 | +var collection = [ |
| 48 | + { path: 'a.md', locals: { date: '2014-01-09' } }, |
| 49 | + { path: 'f.md', locals: { date: '2014-01-02' } }, |
| 50 | + { path: 'd.md', locals: { date: '2013-05-06' } }, |
| 51 | + { path: 'e.md', locals: { date: '2015-01-02' } }, |
| 52 | + { path: 'b.md', locals: { date: '2012-01-02' } }, |
| 53 | + { path: 'f.md', locals: { date: '2014-06-01' } }, |
| 54 | + { path: 'c.md', locals: { date: '2015-04-12' } }, |
| 55 | + { path: 'g.md', locals: { date: '2014-02-02' } }, |
| 56 | +]; |
| 57 | +``` |
| 58 | + |
| 59 | +and you want to sort them by `locals.date`: |
| 60 | + |
| 61 | +```js |
| 62 | +arraySort(collection, 'locals.date'); |
| 63 | +``` |
| 64 | + |
| 65 | +**Result** |
| 66 | + |
| 67 | +```js |
| 68 | +[ |
| 69 | + { path: 'b.md', locals: { date: '2012-01-02' } }, |
| 70 | + { path: 'd.md', locals: { date: '2013-05-06' } }, |
| 71 | + { path: 'f.md', locals: { date: '2014-01-02' } }, |
| 72 | + { path: 'a.md', locals: { date: '2014-01-09' } }, |
| 73 | + { path: 'g.md', locals: { date: '2014-02-02' } }, |
| 74 | + { path: 'f.md', locals: { date: '2014-06-01' } }, |
| 75 | + { path: 'e.md', locals: { date: '2015-01-02' } }, |
| 76 | + { path: 'c.md', locals: { date: '2015-04-12' } } |
| 77 | +]; |
| 78 | +``` |
| 79 | + |
| 80 | +### Sort by multiple properties and functions |
| 81 | + |
| 82 | +Here are the "posts" we want to sort: |
| 83 | + |
| 84 | +```js |
| 85 | +var posts = [ |
| 86 | + { path: 'a.md', locals: { date: '2014-01-01', foo: 'zzz', bar: 1 } }, |
| 87 | + { path: 'f.md', locals: { date: '2014-01-01', foo: 'mmm', bar: 2 } }, |
| 88 | + { path: 'd.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 3 } }, |
| 89 | + { path: 'i.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 5 } }, |
| 90 | + { path: 'k.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 1 } }, |
| 91 | + { path: 'j.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 4 } }, |
| 92 | + { path: 'h.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 6 } }, |
| 93 | + { path: 'l.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 7 } }, |
| 94 | + { path: 'e.md', locals: { date: '2015-01-02', foo: 'aaa', bar: 8 } }, |
| 95 | + { path: 'b.md', locals: { date: '2012-01-02', foo: 'ccc', bar: 9 } }, |
| 96 | + { path: 'f.md', locals: { date: '2014-06-01', foo: 'rrr', bar: 10 } }, |
| 97 | + { path: 'c.md', locals: { date: '2015-04-12', foo: 'ttt', bar: 11 } }, |
| 98 | + { path: 'g.md', locals: { date: '2014-02-02', foo: 'yyy', bar: 12 } }, |
| 99 | +]; |
| 100 | +``` |
| 101 | + |
| 102 | +**The goal** |
| 103 | + |
| 104 | +Sort "posts" by the following: |
| 105 | + |
| 106 | +- `locals.date`: our first choice |
| 107 | +- `locals.foo`: Use this when mulitiple posts have the same `locals.date` value |
| 108 | +- `locals.bar`: Use this when mulitiple posts have the same `locals.foo` value |
| 109 | + |
| 110 | + |
| 111 | +```js |
| 112 | +// let's create a reusable comparison function |
| 113 | +var compare = function(prop) { |
| 114 | + // the last arg, `fn` is the internal comparison function |
| 115 | + // used by the lib. it's exposed here for convenience |
| 116 | + return function (a, b, fn) { |
| 117 | + var valA = get(a, prop); |
| 118 | + var valB = get(b, prop); |
| 119 | + return fn(valA, valB); |
| 120 | + }; |
| 121 | +}; |
| 122 | +``` |
| 123 | + |
| 124 | +**Time to sort!** |
| 125 | + |
| 126 | +```js |
| 127 | +// the comparison args can be an array or a list, makes no difference |
| 128 | +arraySort(posts, ['locals.date', compare('locals.foo'), compare('locals.bar')]); |
| 129 | +``` |
| 130 | + |
| 131 | +**Result** |
| 132 | + |
| 133 | +```js |
| 134 | +[ |
| 135 | + { path: 'b.md', locals: { date: '2012-01-02', foo: 'ccc', bar: 9 } }, |
| 136 | + { path: 'f.md', locals: { date: '2014-01-01', foo: 'mmm', bar: 2 } }, |
| 137 | + { path: 'k.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 1 } }, |
| 138 | + { path: 'd.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 3 } }, |
| 139 | + { path: 'j.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 4 } }, |
| 140 | + { path: 'i.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 5 } }, |
| 141 | + { path: 'h.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 6 } }, |
| 142 | + { path: 'l.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 7 } }, |
| 143 | + { path: 'a.md', locals: { date: '2014-01-01', foo: 'zzz', bar: 1 } }, |
| 144 | + { path: 'g.md', locals: { date: '2014-02-02', foo: 'yyy', bar: 12 } }, |
| 145 | + { path: 'f.md', locals: { date: '2014-06-01', foo: 'rrr', bar: 10 } }, |
| 146 | + { path: 'e.md', locals: { date: '2015-01-02', foo: 'aaa', bar: 8 } }, |
| 147 | + { path: 'c.md', locals: { date: '2015-04-12', foo: 'ttt', bar: 11 } } |
| 148 | +]); |
| 149 | +``` |
| 150 | + |
| 151 | +### Custom functions |
| 152 | + |
| 153 | +If custom functions are supplied, array elements are sorted according to the return value of the compare function. See the [docs for `Array.sort()`][mozilla]. |
| 154 | + |
| 155 | + |
| 156 | +## Related projects |
| 157 | +{%= related(verb.related.list, {remove: name}) %} |
| 158 | + |
| 159 | +## Running tests |
| 160 | +{%= include("tests") %} |
| 161 | + |
| 162 | +## Contributing |
| 163 | +{%= include("contributing") %} |
| 164 | + |
| 165 | +## Author |
| 166 | +{%= include("author") %} |
| 167 | + |
| 168 | +## License |
| 169 | +{%= copyright() %} |
| 170 | +{%= license() %} |
| 171 | + |
| 172 | +*** |
| 173 | + |
| 174 | +{%= include("footer") %} |
| 175 | + |
| 176 | +[mozilla]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort |
| 177 | + |
| 178 | +{%= reflinks(['verb']) %} |
0 commit comments