Skip to content

Commit 6d23c88

Browse files
committed
adds examples
1 parent 8d160bb commit 6d23c88

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

examples/blog-posts.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var arraySort = require('..');
2+
3+
var posts = [
4+
{ path: 'c.md', locals: { date: '2014-01-09' } },
5+
{ path: 'a.md', locals: { date: '2014-01-02' } },
6+
{ path: 'b.md', locals: { date: '2013-05-06' } },
7+
];
8+
9+
// by `locals.date`
10+
console.log(arraySort(posts, 'locals.date'));
11+
12+
// by `path`
13+
console.log(arraySort(posts, 'path'));
14+

examples/custom-function.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var arraySort = require('..');
2+
3+
var arr = [
4+
{one: 'w', two: 'b'},
5+
{one: 'z', two: 'a'},
6+
{one: 'x', two: 'c'},
7+
{one: 'y', two: 'd'},
8+
];
9+
10+
function compare(prop) {
11+
return function (a, b) {
12+
return a[prop].localeCompare(b[prop]);
13+
};
14+
}
15+
16+
var result = arraySort(arr, function (a, b) {
17+
return a.two.localeCompare(b.two);
18+
});
19+
20+
console.log(result);
21+
// Results in:
22+
// [ { one: 'z', two: 'a' },
23+
// { one: 'w', two: 'b' },
24+
// { one: 'x', two: 'c' },
25+
// { one: 'y', two: 'd' } ]

examples/custom-functions.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var arraySort = require('..');
2+
3+
var arr = [
4+
{foo: 'w', bar: 'y', baz: 'w'},
5+
{foo: 'x', bar: 'y', baz: 'w'},
6+
{foo: 'x', bar: 'y', baz: 'z'},
7+
{foo: 'x', bar: 'x', baz: 'w'},
8+
];
9+
10+
function compare(prop) {
11+
return function (a, b) {
12+
return a[prop].localeCompare(b[prop]);
13+
};
14+
}
15+
16+
console.log(arraySort(arr, compare('foo'), compare('bar'), compare('baz')));
17+
18+
// Results in:
19+
// [ { foo: 'w', bar: 'y', baz: 'w' },
20+
// { foo: 'x', bar: 'x', baz: 'w' },
21+
// { foo: 'x', bar: 'y', baz: 'w' },
22+
// { foo: 'x', bar: 'y', baz: 'z' } ]

examples/multiple-props.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var arraySort = require('..');
2+
3+
var posts = [
4+
{ locals: { foo: 'bbb', date: '2013-05-06' }},
5+
{ locals: { foo: 'aaa', date: '2012-01-02' }},
6+
{ locals: { foo: 'ccc', date: '2014-01-02' }},
7+
{ locals: { foo: 'ccc', date: '2015-01-02' }},
8+
{ locals: { foo: 'bbb', date: '2014-06-01' }},
9+
{ locals: { foo: 'aaa', date: '2014-02-02' }},
10+
];
11+
12+
// sort by `locals.foo`, then `locals.date`
13+
var result = arraySort(posts, ['locals.foo', 'locals.date']);
14+
15+
console.log(result);
16+
// [ { locals: { foo: 'aaa', date: '2012-01-02' } },
17+
// { locals: { foo: 'aaa', date: '2014-02-02' } },
18+
// { locals: { foo: 'bbb', date: '2013-05-06' } },
19+
// { locals: { foo: 'bbb', date: '2014-06-01' } },
20+
// { locals: { foo: 'ccc', date: '2014-01-02' } },
21+
// { locals: { foo: 'ccc', date: '2015-01-02' } } ]

0 commit comments

Comments
 (0)