JavaScript Sort an array of strings based on an array of numbers sort array strings javascript,sort array strings java,sort array


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!

Answer:1

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);
Answer:2

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);
Answer:3

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);
Answer:4

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 ...

  1. call javascript function mvc controller
  2. call javascript function mvc view
  3. javascript function in mvc view
  4. return javascript function mvc
  5. call javascript function mvc
  6. javascript function inside mvc
  7. javascript call c# function mvc
  8. onclick call javascript function mvc
  9. call javascript function from mvc razor view
  10. mvc javascript function not defined
  11. pass model to javascript function mvc
  12. onclick javascript function in mvc
  13. call javascript function actionlink mvc
  14. add javascript function to mvc view
  15. execute javascript function from mvc controller
  16. call javascript function in mvc razor
  17. mvc javascript function in partial view
  18. call javascript function from mvc action
  19. mvc javascript function parameters
  20. call javascript function from mvc button

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]...

  1. loop through number javascript
  2. loop through number python
  3. loop through number js
  4. loop through numbers bash
  5. loop through number php
  6. loop through number jquery
  7. loop through number c++
  8. loop through number excel
  9. javascript loop through number range
  10. python loop through number range
  11. react loop through number
  12. bash loop through number range
  13. angular loop through number
  14. loop through a number
  15. typescript loop through number
  16. ruby loop through number
  17. vba loop through number
  18. scala loop through number
  19. vba loop through number range
  20. python loop through number of rows

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 ...

  1. loop through video output
  2. loop through video
  3. python loop through video frames