2

I have two arrays,

A = [green, blue, red];

B = [2, 1, 0]

I want to change the order of A to be in [red, blue, green]. The changes are based on the value of B Below is what I have tried,

arrangeValues();
function arrangeValues() {
    A.sort((a,b)=>{
        let orderA:any=B.indexOf(a.value);
        let orderB:any=B.indexOf(b.value);
        if (orderA==-1)
            orderA=99999;
        if (orderB==-1)
            orderB=99999;
        return orderB-orderA
    })
}
10
  • 1
    The order of b doesn't make sense with the expected output, that would be [red,blue,green] Commented Jul 7, 2018 at 14:38
  • Is B always the same length as A and made up of consecutive numbers starting at 0? Commented Jul 7, 2018 at 14:39
  • 2 in B refers to green, so greens index should be 2 Commented Jul 7, 2018 at 14:39
  • Yes both values should have equal length Commented Jul 7, 2018 at 14:40
  • Then you'll need to specify -- see the answer below, right now we can't tell what you want exactly Commented Jul 7, 2018 at 14:40

4 Answers 4

3

I want to change the order of A to be in [red, blue, green]

The simplest way is to create a new array, using the indexes from B, and taking values from A based on where you are in B:

const A = ["green", "blue", "red"];
const B = [2, 1, 0];
const result = [];
B.forEach((entry, index) => {
  result[entry] = A[index];
});
console.log(result);

Just to be sure that really does what you want (because it was actually a typo on my part, I meant to do something different but seem to have accidentally done what you wanted), here are all six combinations:

const A = ["green", "blue", "red"];

function test(B) {
  const result = [];
  B.forEach((entry, index) => {
    result[entry] = A[index];
  });
  console.log(B.join(", "), "=>", result.join(", "));
}
for (let i = 0; i < A.length; ++i) {
  for (let j = 0; j < A.length; ++j) {
    if (j != i) {
      for (let k = 0; k < A.length; ++k) {
        if (i != k && j != k) {
          test([i, j, k]);
        }
      }
    }
  }
}
.as-console-wrapper {
  max-height: 100% !important;
}

Here's are all six doing what I meant to do (which is what zvona did, zvona's is cleaner):

const A = ["green", "blue", "red"];

function test(B) {
  const result = [];
  B.forEach((entry, index) => {
    result[index] = A[entry];
  });
  console.log(B.join(", "), "=>", result.join(", "));
}
for (let i = 0; i < A.length; ++i) {
  for (let j = 0; j < A.length; ++j) {
    if (j != i) {
      for (let k = 0; k < A.length; ++k) {
        if (i != k && j != k) {
          test([i, j, k]);
        }
      }
    }
  }
}
.as-console-wrapper {
  max-height: 100% !important;
}

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

2 Comments

@ T.J what is ab
@saila - It's a joke: "abuse" vs. "use".
1

You can do that using a simple for loop something like:

function arrangeValues(A,B) {
  var sorted = [];
  for(var i=0;i<A.length;i++){
      sorted.push(A[B[i]]);
  }
  return sorted;
}

Here is a live example:

function arrangeValues(A,B) {
  var sorted = [];
  for(var i=0;i<A.length;i++){
      sorted.push(A[B[i]]);
  }
  return sorted;
}

var A = ["green", "blue", "red"];
var B = [2, 1, 0];
var result = arrangeValues(A,B);
console.log(result);

Comments

1

Simple way:

const A: Array<string> = ["green", "blue", "red"];
const B: Array<number> = [2, 1, 0];
const result: Array<string> = B.map((entry: number) => A[entry]);

Live Example:

const A = ["green", "blue", "red"];
const B = [2, 1, 0];
const result = B.map((entry) => A[entry]);
console.log(result);

All six combinations using this approach:

const A = ["green", "blue", "red"];
function test(B) {
  const result = B.map((entry) => A[entry]);
  console.log(B.join(", "), "=>", result.join(", "));
}
for (let i = 0; i < A.length; ++i) {
  for (let j = 0; j < A.length; ++j) {
    if (j != i) {
      for (let k = 0; k < A.length; ++k) {
        if (i != k && j != k) {
          test([i, j, k]);
        }
      }
    }
  }
}

2 Comments

If the original approach of sorting A based on the corresponding B was correct – and the comment “2 in B refers to green, so greens index should be 2” suggests it was – this won’t do the same thing in general. [1, 2, 0] is blue-red-green if it’s a sequence of indexes to read and red-green-blue if it’s the target index for a given element, for example.
@saila: You should try them out on different arrays first, because they don’t do the same thing.
0

Map the corresponding colors from A to values from B by index and sort by them:

const A = ['green', 'blue', 'red'];

const B = [2, 1, 0];

const sorted = A.sort((a,b) => {

    const [aV,bV] = [a,b].map((_,i) => B[i]);

    return aV - bV;

})

console.log(sorted);

Or if we can assume that B contains indexes, then a simple map would do the job:

const A = ['green', 'blue', 'red'];

const B = [2, 1, 0];

const result = B.map(i => A[i]);

console.log(result);

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.