My Question
How do I sort an array of strings by by another array of strings in Javascript?
Background
I am looking for a way to sort an array of strings via another array. Specifically I have a problem where I cannot sort alphabetically or numerically. I know that Javascript supports custom sort functions as seen in this example:
let numberArray = [40, 1, 5, 200];
function compareNumbers(a, b) {
return a - b;
}
numberArray.sort(compareNumbers);
I am not sure how a custom string comparison function would work for the problem I am looking at. I have given two example arrays in part 1 and part 2 below. If you need further clarification please ask.
PART 1
Example arrays: Note that educationSorted
is desired result of sorting education
by educationLevel
.
let education = [
"High School",
"Middle School",
"Undergrad",
"Middle School",
"Middle School",
"Middle School",
"Middle School",
"Graduate",
"Elementary",
"Elementary",
"Elementary",
"Elementary"
];
let educationLevel = ["Elementary", "Middle School", "High School", "Undergrad", "Graduate"];
let educationSorted = [
"Elementary",
"Elementary",
"Elementary",
"Elementary",
"Middle School",
"Middle School",
"Middle School",
"Middle School",
"Middle School",
"High School",
"Undergrad",
"Graduate"
];
PART 2
Same general problem but the arrays of strings is replaced by an array of arrays.
let education = [
["Bob", "High School"],
["Alice", "Middle School"],
["Jane Doe", "Undergrad"],
["alex", "Middle School"],
["Mega Alice", "Middle School"],
["Jhon Doe", "Middle School"],
["Minor Bob", "Middle School"],
["Chicken", "Graduate"],
["Minor Chicken", "Elementary"],
["Clones", "Elementary"],
["Clones", "Elementary"],
["Clones", "Elementary"]
];
let educationLevel = ["Elementary", "Middle School", "High School", "Undergrad", "Graduate"];
let education = [
["Clones", "Elementary"],
["Clones", "Elementary"],
["Clones", "Elementary"],
["Minor Chicken", "Elementary"],
["Alice", "Middle School"],
["Mega Alice", "Middle School"],
["Jhon Doe", "Middle School"],
["Minor Bob", "Middle School"],
["alex", "Middle School"],
["Bob", "High School"],
["Jane Doe", "Undergrad"],
["Chicken", "Graduate"]
];