Last active
May 19, 2019 00:41
-
-
Save simonschwartz/69f8798631957d198ccd588b632f0587 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// given a database of global parcels like this... | |
const allGlobalParcels = [ | |
{ | |
created: 576424800000, | |
location: "aus", | |
properties: { ... }, | |
}, | |
{ | |
created: 1558163267311, | |
location: "us", | |
properties: { ... }, | |
}, | |
...2701201201 more items | |
]; | |
const sortParcelsByCountry = (parcels, country, order) => { | |
// 1. Filter our list to only include parcels from 'country; | |
const countryParcels = parcels.filter(parcel => parcel.location === country); | |
// 2. Sort the list of parcels by date created | |
const sortedResult = [...countryParcels].sort((a, b) => { | |
if (order === "ascending") return a.created - b.created; | |
// by default return packages by descending order | |
return b.created - a.created; | |
}); | |
return sortedResult; | |
}; | |
const ausParcelsAsc = sortParcelsByCountry(allGlobalParcels, "aus", "ascending"); | |
const ausParcelsDsc = sortParcelsByCountry(allGlobalParcels, "aus", "descending"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment