I found this answer to sort an array based on another array. However, In this answer, when the array is not matched, it's added to first. Instead, I want the unmatched to add to the last. For that what change I need to make?
https://stackoverflow.com/a/28377564/890082
var reference_array = ["bob", "dan", "steven", "corbin"];
var array = ["ryan", "corbin", "steven", "dan", "bob"];
array.sort(function(a, b) {
return reference_array.indexOf(a) - reference_array.indexOf(b);
});
console.log(array);
Current output
["ryan", "bob", "dan", "steven", "corbin"]
^^^^
I want the output to be
["bob", "dan", "steven", "corbin", "ryan"]
^^^^
How can I do that?