javascript lodash sortBy 然后 groupBy,是否保持顺序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28860526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
lodash sortBy then groupBy, is order maintained?
提问by kand
I'm having trouble figuring out from the lodash documentation if my assumption about sorting and grouping is correct.
如果我对排序和分组的假设是正确的,我很难从 lodash 文档中找出答案。
If I use sortBy, then use groupBy, do the arrays produced by groupBy maintain the sort order of items?
如果我使用sortBy,然后使用groupBy,groupBy生成的数组是否保持项目的排序顺序?
For example, say I have the following array:
例如,假设我有以下数组:
var testArray = [[5,6],[1,3],[5,4],[5,1]]
And I would like to group these by their first element, but also have them sorted by their second element within these groups. So, in lodash I assume I can do the following:
我想按它们的第一个元素对它们进行分组,但也让它们按这些组中的第二个元素排序。因此,在 lodash 中,我假设我可以执行以下操作:
_.chain(testArray)
.sortBy(function (item) { return item[1]; })
.groupBy(function (item) { return item[0]; })
.value()
Which ends up producing what I would expect it to:
最终产生了我期望的结果:
{
1: [[1,3]]
5: [[5,1],[5,4],[5,6]]
}
Is this just coincidence? Is there anything about how sortBy and groupBy work that ensures this ordering of the grouped arrays? The documentation says that sortBy is a stable sort, does that in the same way apply to groupBy? Is there any reason I should not assume this will work every time?
这只是巧合吗?有没有关于 sortBy 和 groupBy 如何工作以确保分组数组的这种排序?文档说 sortBy 是一种稳定的排序,这是否同样适用于 groupBy?有什么理由我不应该假设这每次都有效?
回答by Hatch
It's not. Here's example, where order is not retained:
不是。这是不保留订单的示例:
const data = [
{
item: 'item1',
group: 'g2'
}, {
item: 'item2',
group: 'g3'
}, {
item: 'item3',
group: 'g1'
}, {
item: 'item4',
group: 'g2'
}, {
item: 'item5',
group: 'g3'
}
]
const groupedItems = _(data).groupBy(item => item.group).value()
In this case one would expect that group order would be: g2, g3, g1 - reality is that they are sorted g1, g2, g3.
在这种情况下,人们会期望组顺序是:g2、g3、g1——现实是它们按 g1、g2、g3 排序。
You can re-sort them with original array though.
不过,您可以使用原始数组对它们进行重新排序。
const groupedItems = _(data)
.groupBy(item => item.group)
.sortBy(group => data.indexOf(group[0]))
.value()
This will ensure original order of items.
这将确保项目的原始顺序。
回答by JLRishe
The current implementationof _.groupBy
is:
在当前实现的_.groupBy
方法是:
// An internal function used for aggregate "group by" operations.
var group = function(behavior) {
return function(obj, iteratee, context) {
var result = {};
iteratee = cb(iteratee, context);
_.each(obj, function(value, index) {
var key = iteratee(value, index, obj);
behavior(result, value, key);
});
return result;
};
};
// Groups the object's values by a criterion. Pass either a string attribute
// to group by, or a function that returns the criterion.
_.groupBy = group(function(result, value, key) {
if (_.has(result, key)) result[key].push(value); else result[key] = [value];
});
Basically it iterates through each of the items in the collection in order (if the collection is array-like, which it would be after a sortBy
), and pushes them to an array based on their key value.
基本上它按顺序遍历集合中的每个项目(如果集合是类似数组的,它将在 a 之后sortBy
),并根据它们的键值将它们推送到一个数组中。
So yes, I'm not sure if this is an "official" characteristic of _.groupBy
, but it does preserve the order of array-like collections, and that's probably unlikely to change.
所以是的,我不确定这是否是 的“官方”特征_.groupBy
,但它确实保留了类似数组的集合的顺序,而且这可能不太可能改变。
回答by Denis535
Function groupBy returns object. Object doesn't save property order. Does JavaScript Guarantee Object Property Order?
函数 groupBy 返回对象。对象不保存属性顺序。 JavaScript 是否保证对象属性顺序?
But group arrays saves order, because thay are added with push function.
但是组数组可以保存顺序,因为它们添加了推送功能。