Last active
December 10, 2024 16:38
-
-
Save simonschwartz/d778e29f35cb0e32bdc804b467664598 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 => { | |
// 1. Filter our list to only include parcels from 'country; | |
const countryParcels = parcels.filter(parcel => parcel.location === country); | |
// we now return a function that sorts the parcels by date created | |
return order => { | |
// 2. Sort the list of packages by date | |
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; | |
}; | |
}; | |
// we create a new function with the filtered list of parcels by country in it's closure scope | |
const sortAusParcelsBy = sortParcelsByCountry(allGlobalParcels)("aus"); | |
const ausParcelsAsc = sortAusParcelsBy("ascending"); | |
const ausParcelsDsc = sortAusParcelsBy("descending"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment