I currently have this function which sort an array alphabetically.
function compare(a,b) {
if (a.subtitle < b.subtitle)
return -1;
if (a.subtitle > b.subtitle)
return 1;
return 0;
}
I need a similar function that sorts the array by another array. I tried writing it myself but i couldn't get my head around it so i ended up with nothing.
Example:
I need array1 to be sorted depending on where that item is in array2.
Array1 = ['quick','fox','the','brown'];
Array2 = ['the','quick','brown','fox'];
There is probably an easy answer that i am not seeing.
Edit:
Also any items that are in array1 that aren't in array 2 can just be tacked onto the end in no particular order or alphabetically whatever is easier.
return 0;is the default here, and the indentation is deceptive. For this reason, use curly-braces with if-statements in javascript.