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?

2
  • 2
    Please read all answers on a question, not just the accepted one. This answer, for instance, shows how to sort in either direction. Commented Jun 4, 2021 at 12:23
  • Do you understand the logic inside the sort, do you know how the indexOf part works? Commented Jun 4, 2021 at 12:24

3 Answers 3

3

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)

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. it did the trick. Can also explain about >>>? Bit confused.
@SurjithSM: yep, just added the link
Perfect. Thank you.
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);

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.