Skip to content

Instantly share code, notes, and snippets.

@samundrak
Created June 4, 2019 08:52
Show Gist options
  • Save samundrak/6931e8b694b330cba3c85364edd0078b to your computer and use it in GitHub Desktop.
Save samundrak/6931e8b694b330cba3c85364edd0078b to your computer and use it in GitHub Desktop.
const collection = [
{ name: 'samundra', rank: 3 },
{ name: 'abina', rank: 5 },
{ name: 'hari', rank: 1 },
{ name: 'bibek', rank: 2 },
{ name: 'suman', rank: 4 },
];
function sortBy(collection, compareBy) {
return collection.sort((item, lastItem) => {
return item[compareBy] > lastItem[compareBy];
});
}
function getAverage(collection, averageBy) {
return sortBy(collection, averageBy).map(item => item[averageBy])[
Math.floor(collection.length / 2)
];
}
console.log(sortBy(collection, 'rank'));
console.log(getAverage(collection, 'rank'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment