Skip to content
This repository was archived by the owner on Mar 12, 2025. It is now read-only.

Commit c3a78f1

Browse files
author
Luke Murray
committed
Added sortBy to README.md
1 parent 30e73e0 commit c3a78f1

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,26 @@ Immutable.from([1, 2, 3]);
119119
Immutable([1, 2, 3])
120120
```
121121

122+
## Immutable.sortBy
123+
124+
JavaScript's Array `sort()` is a mutating function and its use will throw an ImmutableError. This has been done rather than override the `sort()` method with one that returns an immutable Array. The rationale for this is that returning an immutable array from the `sort()` function would be a surprising change to the expected behavior for developers unfamiliar with immutables. However, a convenient method for creating a sorted immutable array from an unsorted one is still desirable. This function closes this gap.
125+
126+
```javascript
127+
var array = Immutable([1, 3, 2]);
128+
var sortedArray = Immutable.sortBy(array);
129+
// returns Immutable([1, 2, 3]);
130+
```
131+
132+
This function also accepts an optional sorting function as per the original `sort()`:
133+
134+
```javascript
135+
var array = Immutable([1, 3, 2]);
136+
var sortedArray = Immutable.sortBy(array, function(a, b) {
137+
return b - a;
138+
});
139+
// returns Immutable([3, 2, 1]);
140+
```
141+
122142
## Immutable Array
123143

124144
Like a regular Array, but immutable! You can construct these by passing

0 commit comments

Comments
 (0)