mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 07:08:01 +00:00
Add Sorting Arrays Of Objects With Lodash as a javascript til
This commit is contained in:
@@ -9,7 +9,7 @@ and pairing with smart people at Hashrocket.
|
|||||||
|
|
||||||
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
|
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
|
||||||
|
|
||||||
_896 TILs and counting..._
|
_897 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -333,6 +333,7 @@ _896 TILs and counting..._
|
|||||||
- [Resolve And Pass Multiple Values From A Then](javascript/resolve-and-pass-multiple-values-from-a-then.md)
|
- [Resolve And Pass Multiple Values From A Then](javascript/resolve-and-pass-multiple-values-from-a-then.md)
|
||||||
- [Running ES6 Specs With Mocha](javascript/running-es6-specs-with-mocha.md)
|
- [Running ES6 Specs With Mocha](javascript/running-es6-specs-with-mocha.md)
|
||||||
- [Scoping Variables With A Block Statement](javascript/scoping-variables-with-a-block-statement.md)
|
- [Scoping Variables With A Block Statement](javascript/scoping-variables-with-a-block-statement.md)
|
||||||
|
- [Sorting Arrays Of Objects With Lodash](javascript/sorting-arrays-of-objects-with-lodash.md)
|
||||||
- [Splat Arguments To A Function](javascript/splat-arguments-to-a-function.md)
|
- [Splat Arguments To A Function](javascript/splat-arguments-to-a-function.md)
|
||||||
- [Spread The Rest With ES6](javascript/spread-the-rest-with-es6.md)
|
- [Spread The Rest With ES6](javascript/spread-the-rest-with-es6.md)
|
||||||
- [String Interpolation With Template Literals](javascript/string-interpolation-with-template-literals.md)
|
- [String Interpolation With Template Literals](javascript/string-interpolation-with-template-literals.md)
|
||||||
|
|||||||
56
javascript/sorting-arrays-of-objects-with-lodash.md
Normal file
56
javascript/sorting-arrays-of-objects-with-lodash.md
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
# Sorting Arrays Of Objects With Lodash
|
||||||
|
|
||||||
|
The [`lodash`](https://lodash.com/) library comes with a couple functions for
|
||||||
|
sorting collections of objects --
|
||||||
|
[`sortBy`](https://lodash.com/docs/4.17.15#sortBy) and
|
||||||
|
[`orderBy`](https://lodash.com/docs/4.17.15#orderBy).
|
||||||
|
|
||||||
|
Consider the following collection of pokemon:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
const pokemon = [
|
||||||
|
{ name: "Pikachu", level: 12 },
|
||||||
|
{ name: "Charmander", level: 12 },
|
||||||
|
{ name: "Squirtle", level: 15 },
|
||||||
|
{ name: "Bulbasaur", level: 11 }
|
||||||
|
];
|
||||||
|
```
|
||||||
|
|
||||||
|
This collection can be sorted in ascending order by the value of a key in the
|
||||||
|
object using `sortBy`.
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
import _sortBy from "lodash/sortBy";
|
||||||
|
|
||||||
|
_sortBy(pokemon, ["level"]);
|
||||||
|
```
|
||||||
|
|
||||||
|
If you want to control whether the sorting is in ascending or descending order,
|
||||||
|
use `orderBy`.
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
import _orderBy from "lodash/orderBy";
|
||||||
|
|
||||||
|
_orderBy(pokemon, ["level"], ["desc"]);
|
||||||
|
```
|
||||||
|
|
||||||
|
You can also do sorting with primary and secondary keys by including two values
|
||||||
|
in the key sort array.
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
import _sortBy from "lodash/sortBy";
|
||||||
|
|
||||||
|
_sortBy(pokemon, ["name", "level"]);
|
||||||
|
```
|
||||||
|
|
||||||
|
And if you want to indpendently control ascending/descending for these as well,
|
||||||
|
you can.
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
import _orderBy from "lodash/orderBy";
|
||||||
|
|
||||||
|
_orderBy(pokemon, ["level", "name"], ["desc", "asc"]);
|
||||||
|
```
|
||||||
|
|
||||||
|
Check out the [live example](https://codesandbox.io/s/jolly-ardinghelli-cem7t)
|
||||||
|
to see it in action.
|
||||||
Reference in New Issue
Block a user