Nin*_*KiD 25 javascript sorting lodash vue.js
当我传递“desc”时,当我调用该函数时,Lodash 中的 sortBy() 不会按降序排序const sortedData = _.sortBy(data, ['rawAvgPrice'], ['desc']);。这对于升序来说效果很好。但不是按降序排列。我已经发布了我编写的排序功能。我读了主题“ lodash multi-column sortByscending ”,但它并没有帮助我解决我的问题。因此决定发布此内容。
/**
* @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);
},
Run Code Online (Sandbox Code Playgroud)
小智 13
您可以尝试使用 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],
Run Code Online (Sandbox Code Playgroud)
你可以在这里查看官方文档Lodash orderBy