简体   繁体   English

JS如何根据另一个数组的排序对一个数组进行排序()

[英]JS how to sort() one array based on another array’s sortation

When A is.sorted(), it becomes 6, 7, 8, so those number get new index values, so do the same for B. get current index of the values inside B, then rearrange it based on A's new arrangement.当 A is.sorted() 时,它变成 6, 7, 8,所以这些数字得到新的索引值,所以对 B 做同样的事情。获取 B 内部值的当前索引,然后根据 A 的新排列重新排列它。 So when A = 6,7,8, B would be u, z, h所以当 A = 6,7,8 时,B 将是 u,z,h

var arrA = [8, 6, 7] // B follows this arr (A)
var arrB = ['h', 'u', 'z'] // B follows A

arrA.sort()
// output: 6, 7, 8

// arrB.followA’s arrangement somehow 
// output: u, z, h


arrA.reverse()
// output: 8, 7, 6

// arrB.follow A’s arrangement
// output: h, z, u


console.log(arrA);
console.log(arrB)

Use a brute force sort routine.使用蛮力排序例程。 Swap elements in "ary", and if those swap, then swap the same indexes in "bry".交换“ary”中的元素,如果这些交换,则交换“bry”中的相同索引。 This way the index swapping will mirror each other.这样索引交换将相互镜像。

var ary = [8, 7, 4, 3, 5, 1, 6];
var bry = ['u', 'z', 'y', 'a', 'b', 'f', 'r'];
var i = 0;
var j = 0;

for (i = 0; i < ary.length-1; i++){
    for (j = 0; j < ary.length-1; j++){
      if (ary[j] > ary[j+1]){
          let bigger = ary[j];
          ary[j] = ary[j+1];
          ary[j+1] = bigger;

     // make bry swap the same indexes as ary above
          bigger = bry[j];
          bry[j] = bry[j+1];
          bry[j+1] = bigger;
      }
    }
}

It's not possible if it is two different arrays.如果是两个不同的 arrays,这是不可能的。 the simple approach to solve ur problem is to convert your second array into an object.解决您的问题的简单方法是将您的第二个数组转换为 object。

let b = {
    8: 'H',
    6: 'U',
    7: 'Z'
}

arrB = arrA.map((val) => return b[val]);

and use this object to get the value for array B based on the values of array A.并使用此 object 根据数组 A 的值获取数组 B 的值。

correct me if I'm wrong如我错了请纠正我

Create a two-dimensional working array, whose elements are pairs of values from arrA and arrB :创建一个二维工作数组,其元素是来自arrAarrB的值对:

var work = [];
arrA.forEach(function( v, i ) {
    work[ i ] = [ arrA[i], arrB[i] ];
});

Now you can arrange work in any order, and the values from arrA and arrB will stay in lockstep:现在您可以按任何顺序安排work ,并且来自arrAarrB的值将保持同步:

work.sort(function(x, y) {
    return Math.sign( x[0], y[0] );
});

(In the above example, we are ordering by the element in slot 0, which is from arrA . To order by the elements in arrB , change 0 to 1.) (在上面的示例中,我们按插槽 0 中的元素排序,该元素来自arrA 。要按arrB中的元素排序,请将 0 更改为 1。)

Now you can alter work , eg:现在您可以更改work ,例如:

work.reverse();

And extract the corresponding elements that were originally from arrA :并提取最初来自arrA的相应元素:

let newAarrA = work.map(function(x) {
    return x[0]; // from arrA
});
console.log(newArrA);

(Change 0 to 1 to get the corresponding elements from arrB instead.) (将 0 更改为 1 以从arrB获取相应的元素。)

You can use a "Sorting with map" technique, you basically use a temporary array that maps arrB 's elements to values of arrA elements and then sort it.您可以使用“使用映射排序”技术,您基本上使用一个临时数组,将arrB的元素映射到arrA元素的值,然后对其进行排序。

 var arrA = [8, 6, 7] // B follows this arr (A) var arrB = ['h', 'u', 'z'] // B follows A var tmpMap = arrB.map((el, i) => ({ index: i, value: arrA[i] })); tmpMap.sort((a, b) => a.value - b.value); var arrB = tmpMap.map(el => arrB[el.index]); console.log(arrB);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM