Created
February 19, 2016 16:13
-
-
Save sohara/95731b426e48d7ece73c 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
var people = [ | |
{ name: 'Sean', id: 23, type: 'engineer'}, | |
{ name: 'Ryan', id:44, type: 'engineer'}, | |
{ name: 'Christina', id:5, type: 'boss'}, | |
{ name: 'Sherard', id: 23, type: 'candidate'}]; | |
var options = { | |
sortField: 'name', | |
sortOrder: ['Ryan', 'Sean', 'Christina', 'Sherard'] | |
}; | |
function reorder(data, options) { | |
// Your function here. | |
var answer = []; | |
for (var i = 0; i < data.length; i++) { | |
for (var j = 0; j < data.length; j++) { | |
var lookingFor = options.sortOrder[i]; | |
if (data[j][options.sortFild] === lookingFor) { | |
answer.push(data[j]); | |
} | |
} | |
} | |
return answer; | |
} | |
function betterReorder(people, options) { | |
var lookup = {}; | |
var answer = []; | |
var finalSort = options.sortOrder; | |
for (var i = 0; i < people.length; i++) | |
lookup[finalSort[i]] = people[i]; | |
} | |
/* { 'Ryan': {} }, | |
'Sean': {} } | |
*/ | |
for (var j = 0; j < finalSort.length; j++) { | |
var key = finalSort[j] | |
// finalSort[j] | |
answer.push(lookup[finalSort[j]); | |
} | |
reorder(people, options); | |
console.log("Hello Sean - can you see this at all?".split(/\w/)); | |
lol christina wtf |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment