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