var groupedData = _.groupBy(data, 'Category');
groupedData = _.sortBy(groupedData, function (f) { return [f[0].CategoryID, f[0].Category].join("_"); });
此返回结果降序。我希望它返回结果升序。
我试图添加sort(),但它说Sort is not a function
。
还有另一种升序排序的方法。
答案 0 :(得分:0)
您在这里没有提供太多信息。我假设您正在使用下划线库。
有关同一问题的答案,请参见How can I do an asc and desc sort using underscore.js?,
另请参见_sortBy下划线库文档:https://underscorejs.org/#sortBy
简而言之,如文档中所述,sortBy()函数以升序返回列表,该列表是通过在iteratee中运行每个值而确定的,其中iteratee是第二个参数,并且可以采用函数的形式或字符串来确定排序;
_.sortBy(list, iteratee, [context])
似乎您正在尝试使用iteratee来操纵值,而不是用于确定排序顺序。
例如,如果要按CategoryId进行排序,则代码应类似于:
var sortedList = _.sortBy(list, 'CategoryId');
或者看起来像
var sortedList = _.sortBy(list, function (item) { return item.CategoryId; });