Skip to content

Commit a1c7138

Browse files
committed
Add Sorting Arrays Of Objects With Lodash as a javascript til
1 parent d4e91bd commit a1c7138

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ and pairing with smart people at Hashrocket.
99

1010
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
1111

12-
_896 TILs and counting..._
12+
_897 TILs and counting..._
1313

1414
---
1515

@@ -333,6 +333,7 @@ _896 TILs and counting..._
333333
- [Resolve And Pass Multiple Values From A Then](javascript/resolve-and-pass-multiple-values-from-a-then.md)
334334
- [Running ES6 Specs With Mocha](javascript/running-es6-specs-with-mocha.md)
335335
- [Scoping Variables With A Block Statement](javascript/scoping-variables-with-a-block-statement.md)
336+
- [Sorting Arrays Of Objects With Lodash](javascript/sorting-arrays-of-objects-with-lodash.md)
336337
- [Splat Arguments To A Function](javascript/splat-arguments-to-a-function.md)
337338
- [Spread The Rest With ES6](javascript/spread-the-rest-with-es6.md)
338339
- [String Interpolation With Template Literals](javascript/string-interpolation-with-template-literals.md)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Sorting Arrays Of Objects With Lodash
2+
3+
The [`lodash`](https://lodash.com/) library comes with a couple functions for
4+
sorting collections of objects --
5+
[`sortBy`](https://lodash.com/docs/4.17.15#sortBy) and
6+
[`orderBy`](https://lodash.com/docs/4.17.15#orderBy).
7+
8+
Consider the following collection of pokemon:
9+
10+
```javascript
11+
const pokemon = [
12+
{ name: "Pikachu", level: 12 },
13+
{ name: "Charmander", level: 12 },
14+
{ name: "Squirtle", level: 15 },
15+
{ name: "Bulbasaur", level: 11 }
16+
];
17+
```
18+
19+
This collection can be sorted in ascending order by the value of a key in the
20+
object using `sortBy`.
21+
22+
```javascript
23+
import _sortBy from "lodash/sortBy";
24+
25+
_sortBy(pokemon, ["level"]);
26+
```
27+
28+
If you want to control whether the sorting is in ascending or descending order,
29+
use `orderBy`.
30+
31+
```javascript
32+
import _orderBy from "lodash/orderBy";
33+
34+
_orderBy(pokemon, ["level"], ["desc"]);
35+
```
36+
37+
You can also do sorting with primary and secondary keys by including two values
38+
in the key sort array.
39+
40+
```javascript
41+
import _sortBy from "lodash/sortBy";
42+
43+
_sortBy(pokemon, ["name", "level"]);
44+
```
45+
46+
And if you want to indpendently control ascending/descending for these as well,
47+
you can.
48+
49+
```javascript
50+
import _orderBy from "lodash/orderBy";
51+
52+
_orderBy(pokemon, ["level", "name"], ["desc", "asc"]);
53+
```
54+
55+
Check out the [live example](https://codesandbox.io/s/jolly-ardinghelli-cem7t)
56+
to see it in action.

0 commit comments

Comments
 (0)