Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support date objects and other non-primitive values #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@ sort = function sort(property, map) {
}

return function fn(a,b) {
var result;
var am = apply(property, objectPath.get(a, property));
var bm = apply(property, objectPath.get(b, property));
var result = 0;
if (am < bm) result = -1;
if (am > bm) result = 1;
if (am === bm) result = 0;
return result * sortOrder;
}
};
Expand Down
21 changes: 21 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,27 @@ describe('Sort(prop, prop)', function () {
assert(array[3].x === 2);
assert(array[3].y === 3);
});

it('supports Date objects', function() {
var date1 = new Date('2018-01-01T00:00:00Z')
var date2 = new Date('2018-01-02T00:00:00Z')
var date3 = new Date('2018-01-03T00:00:00Z')

var array = [
{ name: 'Hummingbird', date: new Date(date1) },
{ name: 'swallow', date: new Date(date2) },
{ name: 'Finch', date: new Date(date1) },
{ name: 'Sparrow', date: new Date(date2) },
{ name: 'cuckoos', date: new Date(date3) }
];

array.sort(sortBy('date', 'name'));
assert(array[0].name === 'Finch');
assert(array[1].name === 'Hummingbird');
assert(array[2].name === 'Sparrow');
assert(array[3].name === 'swallow');
assert(array[4].name === 'cuckoos');
});
});

describe('Sort(-prop)', function () {
Expand Down