当我将函数作为const sortedData = _.sortBy(data, ['rawAvgPrice'], ['desc']);
调用时,当我传递'desc‘时,Lodash中的sortBy()没有按降序排序。这可以很好地处理升序。但不是降序的。我已经发布了我写的排序功能。我读了"lodash multi-column sortBy descending“这个帖子,但它对我的问题没有帮助。因此决定张贴这篇文章。
/**
* @param {String} element - sorting object element name.
* @param {String} dir - the direction of the sort ASC/DESC.
* @param {Boolean} flag - Signaling to sort the table and update.
*/
sortTableNew(element, dir, flag) {
let direction = dir;
// Change the sorting from ASC to DESC/ DESC to ASC
if (flag) {
direction = dir === 'asc' ? 'desc' : 'asc';
}
// Getting the current open tabs details
const { activeTab } = this.$props.state.keywordSearch;
const { data } = this.$props.state.keywordSearch.tabs[activeTab];
const sortedData = _.sortBy(data, [element], [direction]);
// Updating the cache to dispatch data to the table
cachedHelper.updateCacheKeyword(cachedHelper.SCREEN_TYPE.keywordSearch, activeTab, sortedData);
cachedHelper.updateCacheSortType(cachedHelper.SCREEN_TYPE.keywordSearch, activeTab, direction, column);
},
发布于 2021-06-03 22:33:27
在lodash documentation中,我们在搜索_.sortBy
时会发现:“创建一个元素数组,按照在每个迭代器中运行集合中每个元素的结果以升序排序。”
由此我们可以看到,_.sortBy
总是以升序返回一个排序的数组。
您可以尝试像这样使用_.orderBy
:_.orderBy(users, 'age', 'desc');
发布于 2021-06-03 22:35:11
您可以尝试使用Lodash的orderBy方法。对我来说很有吸引力。
var users = [
{ 'user': 'fred', 'age': 48 },
{ 'user': 'barney', 'age': 34 },
{ 'user': 'fred', 'age': 40 },
{ 'user': 'barney', 'age': 36 }
];
// Sort by `user` in ascending order and by `age` in descending order.
_.orderBy(users, ['user', 'age'], ['asc', 'desc']);
// => objects for [['barney', 36], ['barney', 34], ['fred', 48],
你可以在这里查看官方文档Lodash orderBy
https://stackoverflow.com/questions/67823035
复制相似问题