Skip to content

Commit

Permalink
adds examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jonschlinkert committed Jul 18, 2015
1 parent 8d160bb commit 6d23c88
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
14 changes: 14 additions & 0 deletions examples/blog-posts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var arraySort = require('..');

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' } },
];

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

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

25 changes: 25 additions & 0 deletions examples/custom-function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var arraySort = require('..');

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);
// Results in:
// [ { one: 'z', two: 'a' },
// { one: 'w', two: 'b' },
// { one: 'x', two: 'c' },
// { one: 'y', two: 'd' } ]
22 changes: 22 additions & 0 deletions examples/custom-functions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var arraySort = require('..');

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'},
];

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

console.log(arraySort(arr, compare('foo'), compare('bar'), compare('baz')));

// Results in:
// [ { foo: 'w', bar: 'y', baz: 'w' },
// { foo: 'x', bar: 'x', baz: 'w' },
// { foo: 'x', bar: 'y', baz: 'w' },
// { foo: 'x', bar: 'y', baz: 'z' } ]
21 changes: 21 additions & 0 deletions examples/multiple-props.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var arraySort = require('..');

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' } } ]

0 comments on commit 6d23c88

Please sign in to comment.