change order of array based on another array

sort array based on another array javascript
sort an array based on another array python
order an array based on another array python
sort array based on another array c
sort array based on another array matlab
sort array based on another array php
sort array based on another array lodash
sort array based on another array swift

Let say we have our array like this:

let myArray = ["A", "B", "C", "D"]

What if we want to modify the order of elements in myArray based on modifier array so that, if myArray includes any element of modifier then we send that element to the end of the myArray

Like this:

let modifier =  ["B"] 
myArray = ["A", "C", "D", "B"] // B is sent to the end of myArray

And if we have this:

let modifier =  ["A", "C"]
myArray = ["B", "D", "A", "C"] // A and C are sent to the end of the array

I have tried looping and checking each array element against another but it went complicated...

Any help would be greatly appreciated.

Very simple.

Step-1: Remove elements of modifier array from original array

 myArray = myArray.filter( (el) => !modifier.includes(el) );

Step-2: Push modifier array into original array

myArray = myArray.concat(modifier)

Update

As per demands in comments by seniors :) If use case is to move multiple data:

var myArray = ["A", "A", "A", "B", "B", "B", "C", "D", "E"];
var modifier = ["A", "B"];

// get static part
staticArray = myArray.filter( (el) => !modifier.includes(el) );

// get moving part
moveableArray = myArray.filter( (el) => modifier.includes(el) );

// merge both to get final array
myArray = staticArray.concat(moveableArray);

console.log(myArray);

Order an array of objects based on another array order · GitHub, Sort array of objects based on another array. */. function mapOrder (array If you really want to add at the END of the array, you change lines 11 to 15 : if (order. I want to change the order of a 1D array based on another 1D array which specifies the order. For example, I have an array which has the following items: 12, 13, 14

You could sort the array and move the items of modifier to the end of the array.

function sort(array, lastValues) {
    var last = Object.fromEntries(lastValues.map((v, i) => [v, i + 1]));
    return array.sort((a, b) => (last[a] || - Infinity) - (last[b] || - Infinity));
}

var array = ["A", "B", "C", "D"];

console.log(...sort(array, ["B"]));
console.log(...sort(array, ["A", "C"]));

Sort an array according to the order defined by another array , Following is C implementation of this method. filter_none. edit close. play_arrow. link brightness_4 code  Sort an array according to the order defined by another array - GeeksforGeeks Sort an array according to the order defined by another array Given two arrays A1 [] and A2 [], sort A1 in such a way that the relative order among the elements will be same as those are in A2. For the elements not present in A2, append them at last in sorted order.

Simply use this to get desired result

    let myArray = ["A", "B", "C", "D"];
let modifier =  ["A", "C"];

for(let i=0;i<modifier.length;i++){
   if(myArray.includes(modifier[i])){
     myArray.splice(myArray.indexOf(modifier[i]), modifier[i]);
     myArray.push(modifier[i]);
   }
}
console.log(myArray);

Sorting an array according to another array using pair in STL , We are given two arrays. We need to sort one array according to another. Examples: Input : 2 1 5 4 9 3 6 7 10 8 A B C D E F G H I J Output : 1 2 3 4 5 6 7 8 9 10  Order an array of objects based on another array order. Raw. mapOrder.js. /**. * Sort array of objects based on another array. */. function mapOrder ( array, order, key) {.

Sorting an array based on another array's order, quickly, I'm going to assume that in the general case these ids might be strings and might be in a non-natural order in both first and second , since  Rearrange array such that even positioned are greater than odd; Rearrange an array in order – smallest, largest, 2nd smallest, 2nd largest, .. Double the first element and move zero to end; Reorder an array according to given indexes; Product of all Subarrays of an Array; Sliding Window Maximum : Set 2; Perfect Sum Problem

Array sorting based on another array? - JavaScript, Is anyone able to help to sort the dates in this format? var dates = [ 'May 24', 'Jun 15', 'Jan 26', 'Nov 7', 'Nov 22', 'Feb 22', 'Oct 21', 'Oct 4', 'Sep  Enter the formula in a cell. Hold down the Ctrl and Shift keys on the keyboard. Press and release the Enter key to create the array formula. Release the Ctrl and Shift keys.

Sort an array based on order defined by another array, Sort array according to another array: Given two arrays, reorder elements of first array by order of elements defined by the second array. Elements which are not  So, to change the positioning of rows based on values returned by argsort(). Pass that to [] operator of 2D numpy array i.e. arr2D[arr2D[:,columnIndex].argsort()] It will change the row order and make the 2D numpy array sorted by 2nd column i.e. by column at index position 1. Let’s see some other examples, Sorting 2D Numpy Array by column at index 0

Comments
  • Brilliant... Thanks
  • Won't work if the elements occur twice.
  • @JonasWilms what is your use case ? Please provide an example.
  • e.g. array=AAABBBCDE, modifier=AB
  • @georg : I have updated my answer with your use case as well.
  • Fun fact: Infinity-Infinity = NaN. Since you already do i+1, why not just 0?