使用javascript sort更改基于另一个数组的顺序

时间:2018-07-07 14:35:10

标签: javascript typescript

我有两个数组,

A = [green, blue, red];

B = [2, 1, 0]

我想将 A 的顺序更改为[红色,蓝色,绿色]。所做的更改基于 B 的值 下面是我尝试过的,

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

4 个答案:

答案 0 :(得分:3)

  

我想将A的顺序更改为[红色,蓝色,绿色]

最简单的方法是使用B中的索引创建一个新数组,并根据您在A中的位置从B中获取值:

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

仅需确保确实可以完成您想要的操作(因为这实际上是我的错别字,我的意思是要做一些不同的操作,但似乎意外地做了您想要的操作),以下是这六个组合:

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

这六个人都在做我 meant 要做的事情(这是what zvona did,zvona比较干净):

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

答案 1 :(得分:1)

您可以使用简单的for循环来实现 像这样:

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

这是一个实时示例:

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

答案 2 :(得分:1)

简单方法:

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

实时示例:

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

使用此方法的所有六个组合:

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

答案 3 :(得分:0)

按索引将A的相应颜色映射到B的值,并按它们排序:

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

或者,如果我们可以假设B包含索引,那么简单的map就可以完成工作:

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

const B = [2, 1, 0];

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

console.log(result);