0

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?

James
  • 20,957
  • 5
  • 26
  • 41
Surjith S M
  • 6,642
  • 2
  • 31
  • 50
  • 2
    Please read all answers on a question, not just the accepted one. [This answer](https://stackoverflow.com/a/28377653/215552), for instance, shows how to sort in either direction. – Heretic Monkey Jun 04 '21 at 12:23
  • Do you understand the logic inside the `sort`, do you know how the indexOf part works? – luk2302 Jun 04 '21 at 12:24

3 Answers3

2

This should do the trick:

var reference_array = ["bob", "dan", "steven", "corbin"];
var array = ["ryan", "corbin",  "steven", "xyz", "dan", "bob", "abc"];

array.sort((a, b) => 
  (reference_array.indexOf(a) >>> 0) -
  (reference_array.indexOf(b) >>> 0));


console.log(array); 

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Unsigned_right_shift . Basically, it leaves positive numbers as is and converts -1 to some big value.

You can also do that without any sorting at all:

var reference_array = ["bob", "dan", "steven", "corbin"];

var array = ["ryan", "corbin",  "steven", "xyz", "dan", "bob", "abc"];


var result = [
  ...reference_array.filter(x => array.includes(x)),
  ...array.filter(x => !reference_array.includes(x))
]
  
console.log(result)
georg
  • 211,518
  • 52
  • 313
  • 390
1

In cases where a/b is not found, you want to hard-code the return value of the sort function to force the element to the end of the array.

var reference_array = ["bob", "dan", "steven", "corbin"];
var array = ["ryan", "corbin",  "steven", "dan", "bob"];
array.sort(function(a, b) {
  const ai = reference_array.indexOf(a);
  const bi = reference_array.indexOf(b);
  if (bi === -1) return -1;
  if (ai === -1) return 1;
  return ai - bi;
});
console.log(array);
James
  • 20,957
  • 5
  • 26
  • 41
1

Please check the question

var reference_array = ["bob", "dan", "steven", "corbin", "test"];
var array = ["ryan", "abc", "corbin", "steven", "dan", "bob"];
array.sort(function(a, b) {
  const a_idx = reference_array.indexOf(a)
  const b_idx = reference_array.indexOf(b);
  if (a_idx >= 0 && b_idx >= 0) return a_idx - b_idx; // normal 
  else if (a_idx < 0 && b_idx >= 0) return 1; // a doesn't not exist reference array and b exists
  else if (a_idx >= 0 && b_idx < 0) return -1; // b doesn't exist and a exists
  else return a.localeCompare(b) // both a, b don't exsit  and compare the string 
});
console.log(array);
Reinis
  • 477
  • 1
  • 5
  • 13