简体   繁体   English

如何根据另一个数组的排序方式对一个数组进行排序? (JavaScript)的

[英]How to sort one array based on how another gets sorted? (Javascript)

I have to create a function to sort a string of numbers based on the 'weight' of each number--the 'weight' is the digits of the numbers added together (the weight of 99 would be 18, the weight of 100 would be 1, etc etc). 我必须创建一个函数来根据每个数字的“权重”对一串数字进行排序 - “权重”是加在一起的数字的数字(99的权重为18,权重为100将是1等等)。 This means that a string "100 54 32 62" would return "100 32 62 54" . 这意味着字符串"100 54 32 62"将返回"100 32 62 54"

I can get an array of the weights of these numbers just fine by using: 我可以使用以下方法获得这些数字的权重数组:

function orderWeight(str) {
    var arr = str.split(" ");
    var sortArr = [];
    arr.forEach(t => sortArr.push(t.split("").map(s => parseInt(s, 10)).reduce(add, 0)));
}

where add is just a generic addition function. 其中add只是一个通用的附加功能。 For the above example, sortArr would be [1, 9, 5, 8] . 对于上面的例子, sortArr将是[1, 9, 5, 8] sortArr [1, 9, 5, 8]

What's the best way to sort the array of the original numbers from the string arr based on how the new array of the number weights sortArr gets sorted? 根据数字权重sortArr的新数组如何排序,从字符串arr中对原始数字数组进行排序的最佳方法是什么?

Thanks! 谢谢!

This should do the trick: 这应该做的伎俩:

var x = '100 54 32 62';

function orderWeight(str) {
  return str.split(' ').sort(function(a, b) {
    return (a.split('').reduce(function(p, c) { return +p + +c; })) > (b.split('').reduce(function(p, c) { return +p + +c; }));
  }).join(' ');
}

var result = orderWeight(x);

Output: 输出:

100 32 62 54

UPDATE: 更新:

Per suggested by Sterling, here's the same function written in lambda format. 根据Sterling的建议,这里是用lambda格式编写的相同函数。

var x = '100 54 32 62';

function orderWeight(str) {
  return str.split(' ').sort((a, b) => a.split('').reduce((p, c) => +p + +c) > b.split('').reduce((p, c) => +p + +c)).join(' ');
}

var result = orderWeight(x);

Note: This is my first time writing Javascript using lambda syntax. 注意:这是我第一次使用lambda语法编写Javascript。 Thanks to Sterling for suggesting. 感谢Sterling的建议。

A solution with sorting with map . 使用地图排序的解决方案。

 function sort(string) { var array = string.split(' '), mapped = array.map(function (a, i) { return { index: i, value: +a.split('').reduce(function (a, b) { return +a + +b; }) }; }); return mapped.sort(function (a, b) { return a.value - b.value; }).map(function (a) { return array[a.index]; }).join(' '); } document.write('<pre>' + JSON.stringify(sort('100 54 32 62'), 0, 4) + '</pre>'); 

I would have an array of indexes (0..n-1) and sort it based on the weights array (by passing a comparison function that compares the weights for the given index values). 我将有一个索引数组(0..n-1)并根据权重数组对其进行排序(通过传递一个比较函数来比较给定索引值的权重)。 Then You will have an array of indexes, which show where each item should be (in your example the index array would be [0, 2, 3, 1] which means that arr[0] should be first, then arr[2], etc. So now you can build the sorted array using the index array, for example [arr[0], arr[2], arr[3], arr[1]]. So to recap: Get the input array, compute weights array, create index array 0..n-1, sort it based on the weights array, and finally construct the output array based on the sorted index array. 然后你将有一个索引数组,显示每个项目应该在哪里(在你的例子中,索引数组将是[0,2,3,1],这意味着arr [0]应该是第一个,然后是arr [2]现在你可以使用索引数组构建排序数组,例如[arr [0],arr [2],arr [3],arr [1]]。所以回顾一下:获取输入数组,计算权重数组,创建索引数组0..n-1,根据权重数组对其进行排序,最后根据排序的索引数组构造输出数组。

暂无
暂无

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

相关问题 在数组中,如何使用.sort()根据一个属性将另一个属性排序的对象组合在一起? - In an array, how do you group together objects based on one property that are being sorted by another, using .sort()? 如何在 JavaScript 中将两个排序数组合并到一个排序数组中而不使用 sort() - how to merge two sorted array in one sorted array in JavaScript without using sort() 如何根据 javascript 中的时间戳对 3 个已排序的 arrays 进行排序 - how to sort 3 sorted arrays based on timestamps in javascript 是否有可能基于JavaScript中的另一个对象对已排序的数组进行排序? - is it possible to sort a sorted array based on another object in javascript? 如何基于另一个JavaScript数组对JavaScript中的数组进行排序? - How to Sort an array in JavaScript based on another JavaScript array? JS如何根据另一个数组的排序对一个数组进行排序() - JS how to sort() one array based on another array’s sortation Javascript-如何根据第二个数组的顺序比较和排序一个数组? - Javascript - How to compare and sort one array based on the order of second array? 如何基于JavaScript中的一列对多维数组进行排序 - How to sort a multidimentional array based on one column in JavaScript 根据javascript中的另一个数组对数组进行排序,同时在末尾保留未排序的元素 - Sort an array based on another array in javascript while keeping not sorted elements at the end 使用javascript基于另一个数组对一个数组进行排序 - Sort one array based on another array using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM