本文介绍了lodash多列sortBy降序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于几个属性对一组对象进行排序有一个很好的方法:

There's a nifty method to sort an array of objects based on several properties:

var data = _.sortBy(array_of_objects, ['type', 'name']);

但这仅适用于升序排序。是否有一些方便的方法来定义每列的方向?例如

However that is only for ascending sorting. Is there some handy way of defining direction per column? E.g.

var data = _.sortBy(array_of_objects, [{'type': 'asc'}, {'name': 'desc'}]);


推荐答案

lodash 3.5开始。 0 您可以使用(更名为 in v4.3.0):

As of lodash 3.5.0 you can use sortByOrder (renamed orderBy in v4.3.0):

var data = _.sortByOrder(array_of_objects, ['type','name'], [true, false]);

由于版本3.10.0 ,您甚至可以使用标准语义进行排序( asc,desc):

Since version 3.10.0 you can even use standard semantics for ordering (asc, desc):

var data = _.sortByOrder(array_of_objects, ['type','name'], ['asc', 'desc']);

在lodash的版本4 中,此方法已重命名为:

In version 4 of lodash this method has been renamed orderBy:

var data = _.orderBy(array_of_objects, ['type','name'], ['asc', 'desc']);

这篇关于lodash多列sortBy降序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 17:34