Last active
February 13, 2020 04:13
-
-
Save thewarpaint/93cf5b76b0078545132cebb677f87852 to your computer and use it in GitHub Desktop.
This file contains 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
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" | |
} | |
]; |
This file contains 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
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