Skip to content

Instantly share code, notes, and snippets.

@thewarpaint
Last active February 13, 2020 04:13
Show Gist options
  • Save thewarpaint/93cf5b76b0078545132cebb677f87852 to your computer and use it in GitHub Desktop.
Save thewarpaint/93cf5b76b0078545132cebb677f87852 to your computer and use it in GitHub Desktop.
function compareNumber(a, b) {
return a - b;
}
function compareString(a, b) {
return a.localeCompare(b);
}
const comparatorMap = {
age: compareNumber,
name: compareString
};
function comparatorFactory(...keys) {
return (a, b) => {
for (const key of keys) {
const comparison = comparatorMap[key](a[key], b[key]);
if (comparison !== 0) {
return comparison;
}
}
return 0;
}
}
let comparator = comparatorFactory('age', 'name');
let people = [
{
"age": 31,
"name": "Eduardo"
},
{
"age": 31,
"name": "Abril"
},
{
"age": 31,
"name": "Zaratustra"
},
{
"age": 13,
"name": "Alan"
},
{
"age": 25,
"name": "Eduardo"
}
];
function lowestUnassignedInt(array) {
array.sort();
let previous = 0;
for (const i of array) {
if (i !== previous + 1) {
break;
}
previous++;
}
return previous + 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment