javascript - 如何使 Lodash sortBy() 对数据进行降序排序?
问题描述
Lodash 中的 sortBy() 没有按降序排序,当我传递 'desc' 时,调用函数为const sortedData = _.sortBy(data, ['rawAvgPrice'], ['desc']);
. 这适用于升序。但不是降序排列。我已经发布了我编写的排序功能。我阅读了线程“ 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);
},
解决方案
推荐阅读
- c - 在指针数组中打印指针指向的值
- arrays - 最终断言未在 C 中传递
- mongodb - Typescript/Mongoose 错误:模型类型上不存在 findUser
- css - 选择上面具有某个类的 CSS 嵌套类
- windows - 收集用户输入不保存输入的值
- jupyter-notebook - Jupyter Notebook、markdown、表格
- python - 使用 beautifulsoup 和 request 解析 HTML 表会得到 empy soup
- pdfbox - 通过pdfbox删除pdf中的一个句子
- amazon-web-services - AWS Amplify 推送不断失败
- python - 熊猫数据框,按另一列中的值排名,不使用 Python FOR 循环