Created
May 23, 2019 21:07
-
-
Save GeoffMahugu/a4ba54dba08e77bb08d27f7f63a871e7 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
// Sample data | |
const data = [ | |
{ name: 'Geoffrey Mahugu', age: 21, gender: 'Male' }, | |
{ name: 'Jane Doe', age: 40, gender: 'Female' }, | |
{ name: 'John Doe', age: 31, gender: 'Male' }, | |
{ name: 'Lucy Nancy', age: 29, gender: 'Female' }, | |
{ name: 'Sandy Glane', age: 32, gender: 'Female' }, | |
]; | |
/* Function that acceps an array of objects and returns a sorted list of | |
Users between 40 - 30 sorted by Gender | |
*/ | |
getGroup(data) { | |
const ageGroup = []; | |
for (const i in data) { | |
if (data[i].age >= 30 && data[i].age <= 40) { | |
ageGroup.push(data[i]); | |
} | |
} | |
const sortedData = ageGroup.sort((a, b) => a.gender.localeCompare(b.gender)); | |
return sortedData; | |
} | |
// Calling the function passing the array | |
getGroup(data); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment