lodash sortBy然后groupBy,是否维持秩序?

时间:2015-03-04 17:06:36

标签: javascript arrays sorting lodash

我无法从lodash文档中找出关于排序和分组的假设是否正确。

如果我使用sortBy,那么使用groupBy,groupBy生成的数组是否保持项目的排序顺序?

例如,假设我有以下数组:

var testArray = [[5,6],[1,3],[5,4],[5,1]]

我想用它们的第一个元素对它们进行分组,但也要按照这些组中的第二个元素对它们进行排序。所以,在lodash我假设我可以做到以下几点:

_.chain(testArray)
  .sortBy(function (item) { return item[1]; })
  .groupBy(function (item) { return item[0]; })
  .value()

最终会产生我期望的结果:

{
  1: [[1,3]]
  5: [[5,1],[5,4],[5,6]]
}

这只是巧合吗?有什么关于sortBy和groupBy如何工作以确保分组数组的这种排序?文档说sortBy是一个稳定的排序,以同样的方式应用于groupBy吗?有什么理由我不应该认为每次都会有效吗?

3 个答案:

答案 0 :(得分:10)

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.

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.

答案 1 :(得分:9)

_.groupBy的{​​{3}}是:

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

基本上,它按顺序遍历集合中的每个项目(如果集合类似于数组,它将在sortBy之后),并根据其键值将它们推送到数组。

所以是的,我不确定这是否是_.groupBy的“官方”特征,但它确实保留了类似数组的集合的顺序,并且这可能不太可能改变。

答案 2 :(得分:1)

功能组返回对象。对象不保存属性顺序。 Does JavaScript Guarantee Object Property Order?

但是组数组保存了顺序,因为它们添加了推送功能。

相关问题