I am trying to do the following operation: sort an array of strings based on an array of numbers of equal length. For example:
A=[a,b,c,d,e]
B=[1,3,2,5,4]
A'=[a,c,b,e,d] //<=The desired outcome
Basically, I am thinking about sorting the array A based on sorting array B into ascending order. Here, I am thinking about implicit value pairs, just like objects.
I can think of creating an object, sorting it, and then separating out the array of strings, but this is probably too much code and am wondering if there is a simpler method to achieve this.
Any advice will be welcome. Thank you very much!
You don't need to sort anything here.
Just use the numbers as "positions" from where to get the value for the current index/position in the result.
const input = ["a", "b", "c", "d", "e"];
const order = [1, 3, 2, 5, 4];
const result = order.map(position => input[position - 1]);
console.log(result);
A little bit of code golf:
const A=['a','b','c','d','e'];
const B=[1,3,2,5,4];
// for this to work, we have to assume A and B have the same length
// if you implement this into a function, do such a check yourself first
const sorted = A.map((item, index, arr) => arr[B[index] - 1]);
console.log(sorted);
If you want to have a custom order, then you can create an object with order:
let a = ['a', 'b', 'c', 'd', 'e'];
let b = [1, 3, 2, 5, 4];
let order = {a: 1, b: 3, c:2, d: 5, e:4};
const result = a.sort((a, b) => order[a] - order[b]);
console.log(result);
const location = 'abc'; const template = <p>Location: {location}</p>; ReactDOM.render(template, document.getElementById('app')); The above code keeps redirecting my page to '/abc'. Babel ...
const location = 'abc'; const template = <p>Location: {location}</p>; ReactDOM.render(template, document.getElementById('app')); The above code keeps redirecting my page to '/abc'. Babel ...
I am developing an Quiz Application in MVC 5. I have added two tables in database. One for marks and other for Questions and Answers. I have entered data in database for question, answers and have ...
I am developing an Quiz Application in MVC 5. I have added two tables in database. One for marks and other for Questions and Answers. I have entered data in database for question, answers and have ...
I have an array such as [2,5,3,1] each number represents an index number. How would I be able to construct an object path using these nodes? For example: [2,5] would create: myObject.conversations[i]...
I have an array such as [2,5,3,1] each number represents an index number. How would I be able to construct an object path using these nodes? For example: [2,5] would create: myObject.conversations[i]...
I have two video sources that are outputted to my page via ajax (the second is hidden). I play the first one and bind an event onto it so that when it has ended the next video is played and shown ...
I have two video sources that are outputted to my page via ajax (the second is hidden). I play the first one and bind an event onto it so that when it has ended the next video is played and shown ...