I'd like to sort this JavaScript array by three values, but I can't seem to figure out how to sort by more than one property at a time.
The requirements are:
featured
items should be at the very top- Then items with no name (
null
) - Then items with a name that are not
featured
- Sort everything by
createdAt
in descending order (newest first)
This is the array:
var items [
{ name: 'foo-1', featured: true, createdAt: 1493000001 },
{ name: null, featured: false, createdAt: 1493000003 },
{ name: 'foo-3', featured: true, createdAt: 1493000002 },
{ name: 'foo-4', featured: false, createdAt: 1493000004 },
{ name: 'foo-5', featured: false, createdAt: 1493000005 },
];
The result should be:
[
{ name: 'foo-3', featured: true, createdAt: 1493000002 },
{ name: 'foo-1', featured: true, createdAt: 1493000001 },
{ name: null, featured: false, createdAt: 1493000003 },
{ name: 'foo-5', featured: false, createdAt: 1493000005 },
{ name: 'foo-4', featured: false, createdAt: 1493000004 },
]