Skip to content

Instantly share code, notes, and snippets.

@simonschwartz
Last active May 19, 2019 00:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonschwartz/69f8798631957d198ccd588b632f0587 to your computer and use it in GitHub Desktop.
Save simonschwartz/69f8798631957d198ccd588b632f0587 to your computer and use it in GitHub Desktop.
// 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